【问题标题】:Union ALL sum results in one row and keep summed valuesUnion ALL sum 结果在一行中并保留求和值
【发布时间】:2022-12-16 13:27:15
【问题描述】:

我正在尝试构建一个查询,该查询获取每个用户不同类型的复制的总和。我想显示一个统一的结果,其中包含每个复制的行和将它们汇总为每个用户的所有行的另一行。这是查询:

SELECT replica_name,
       user_id,
       short_name,
       number_of_replications,
       firstReplication,
       lastReplication
FROM   (SELECT 'Initial'            AS replica_name,
               sc.user_id           AS user_id,
               u.short_name         AS short_name,
               Count(sc.user_id)    AS number_of_replications,
               Min(sc.connected_at) AS firstReplication,
               Max(sc.connected_at) AS lastReplication
        FROM   phoenix.synchro_connections sc
               JOIN phoenix.users u
                 ON u.user_id = sc.user_id
        WHERE  Lower(sc.synchro_type) = 'initial'
               AND  sc.size_in_bytes IS NOT NULL
        GROUP  BY sc.user_id,
                  u.short_name,
                  sc.synchro_type
        UNION ALL
        SELECT 'Delta'              AS replica_name,
               sc.user_id           AS user_id,
               u.short_name         AS short_name,
               Count(sc.user_id)    AS number_of_replications,
               Min(sc.connected_at) AS firstReplication,
               Max(sc.connected_at) AS lastReplication
        FROM   phoenix.synchro_connections sc
               JOIN phoenix.users u
                 ON u.user_id = sc.user_id
        WHERE  Lower(sc.synchro_type) = 'delta'
               AND  sc.size_in_bytes IS NOT NULL
        GROUP  BY sc.user_id,
                  u.short_name,
                  sc.synchro_type
        UNION ALL
        SELECT 'All'                AS replica_name,
               sc.user_id           AS user_id,
               u.short_name         AS short_name,
               Count(sc.user_id)    AS number_of_replications,
               Min(sc.connected_at) AS firstReplication,
               Max(sc.connected_at) AS lastReplication
        FROM   phoenix.synchro_connections sc
               JOIN phoenix.users u
                 ON u.user_id = sc.user_id
        WHERE  Lower(sc.synchro_type) <> 'upload'
               AND  sc.size_in_bytes IS NOT NULL
        GROUP  BY sc.user_id,
                  u.short_name,
                  sc.synchro_type) AS t
WHERE  short_name = 'BY060955'
ORDER  BY replica_name ASC,
          number_of_replications DESC

这是结果:

replica_name user_id short_name number_of_replications firstReplication             LastReplication
All          22472   BY060955   836                    2022-11-14 06:26:05.2415463  2022-12-08 10:25:17.4282712
All          22472   BY060955   2                      2022-11-14 06:25:08.2385837  2022-11-16 11:55:41.0263526
Delta        22472   BY060955   836                    2022-11-14 06:26:05.2415463  2022-12-08 10:25:17.4282712
Initial      22472   BY060955   2                      2022-11-14 06:25:08.2385837  2022-11-16 11:55:41.0263526

我希望“全部”成为单行 (836+2 = 838),但“增量”和“初始”保持原样

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    从最后一个查询中,删除分组 sc.synchro_type

    SELECT 'All'                AS replica_name,
               sc.user_id           AS user_id,
               u.short_name         AS short_name,
               Count(sc.user_id)    AS number_of_replications,
               Min(sc.connected_at) AS firstReplication,
               Max(sc.connected_at) AS lastReplication
        FROM   phoenix.synchro_connections sc
               JOIN phoenix.users u
                 ON u.user_id = sc.user_id
        WHERE  Lower(sc.synchro_type) <> 'upload'
               AND  sc.size_in_bytes IS NOT NULL
        GROUP  BY sc.user_id,
                  u.short_name
    

    您应该能够以更好的方式计算上述内容,只读取表中的行。如果您需要以相同方式获得结果,则可以使用UNPIVOT

    SELECT sc.user_id AS user_id,
           --
           SUM(IIF(Lower(sc.synchro_type) = 'initial', 1, 0)) AS initial_number_of_replications,
           MIN(IIF(Lower(sc.synchro_type) = 'initial', sc.connected_at, NULL)) AS initial_firstReplication,
           MAX(IIF(Lower(sc.synchro_type) = 'initial', sc.connected_at, NULL)) AS initial_lastReplication,
           --
           SUM(IIF(Lower(sc.synchro_type) = 'delta', 1, 0)) AS delta_number_of_replications,
           MIN(IIF(Lower(sc.synchro_type) = 'delta', sc.connected_at, NULL)) AS delta_firstReplication,
           MAX(IIF(Lower(sc.synchro_type) = 'delta', sc.connected_at, NULL)) AS delta_lastReplication,
           --
           SUM(IIF(Lower(sc.synchro_type) <> 'upload', 1, 0)) AS ALL_number_of_replications,
           MIN(IIF(Lower(sc.synchro_type) <> 'upload', sc.connected_at, NULL)) AS ALL_firstReplication,
           MAX(IIF(Lower(sc.synchro_type) <> 'upload', sc.connected_at, NULL)) AS ALL_lastReplication
    FROM phoenix.synchro_connections sc
    INNER JOIN phoenix.users u
        ON u.user_id = sc.user_id
    WHERE Lower(sc.synchro_type) <> 'upload'
        AND sc.size_in_bytes IS NOT NULL
        AND u.short_name = 'BY060955'
    GROUP  BY sc.user_id
    
                  
    

    【讨论】:

    • Мерси, брато... изобщо не бях видял, че така съм го направил... 谢谢,没看到我是那样做的....
    • за жалост нещо не можах да се справя... ако е удобно, би ли ми показал как по-точно трябва да стане :)
    • @BoyanMihnev 检查编辑。如果您有任何问题,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    相关资源
    最近更新 更多