汇总未按预期汇总

时间:2019-06-06 21:03:18

标签: sql sql-server

情况:

我想在输出的末尾使用Rollup,但似乎多次添加相同的数字,因此使总额增加了。

我的查询如下:

; with cte as (
    SELECT
          cast(point.date as date) AS target_date
        , 'all' AS [site]
        , 'pc' AS device
        , point.id
        , point.category
        , point.command 
        , staff
        , point.point
    FROM table.banana  AS point
    LEFT JOIN table.orange AS staff ON point.id=staff.id
    WHERE
       CAST(point.date AS date) = '2019-05-26' 
     )


 -- base query 
     SELECT
          base.target_date
        , base.[site]
        , base.device
        , SUM(case when cte.category = 'Bonus' 
                and cte.type = 'free' 
                and cte.command = 'add' 
                and cte.staff is null then cte.point else 0 end) as total
    FROM ( -- this is just a mapping
        SELECT
              (SELECT CAST(key_value AS DATE) FROM baseDate WHERE [KEY_NAME] = 'target_date') AS target_date 
            , [target].[site]
            , [target].[device]
        FROM ( 
            SELECT 'net' AS [site], 'pc' AS device
            UNION ALL SELECT 'net','android_app'
            UNION ALL SELECT 'com','pc'
            UNION ALL SELECT 'com','android_app'
        ) AS [target]) AS base
    LEFT JOIN cte 
        ON base.target_date = cte.target_date 
            AND base.device = cte.device
    GROUP BY      
          base.target_date
        , base.[site]
        , base.device
    WITH ROLLUP 
    GO

我的输出是:

+-------------+------+-------------+----------------------------+
| target_date | site |   device    |           total             |
+-------------+------+-------------+----------------------------+
| 5/26/2019   | com  | android_app |                       -    |
| 5/26/2019   | com  | pc          |                200,000.00  |
| 5/26/2019   | com  | NULL        |                200,000.00  |
| 5/26/2019   | net  | android_app |                       -    |
| 5/26/2019   | net  | pc          |                200,000.00  |
| 5/26/2019   | net  | NULL        |                200,000.00  |
| 5/26/2019   | NULL | NULL        |                400,000.00  |
| NULL        | NULL | NULL        |                400,000.00  |
+-------------+------+-------------+----------------------------+

我的预期输出应为: 请注意,该结构看起来是这样的,因为我必须将10个其他视图加入基本查询中。

+-------------+------+-------------+----------------------------+
| target_date | site |   device    |            total           |
+-------------+------+-------------+----------------------------+
| 5/26/2019   | com  | android_app |                       -    |
| 5/26/2019   | com  | pc          |                200,000.00  |
| 5/26/2019   | com  | NULL        |                200,000.00  |
| 5/26/2019   | net  | android_app |                       -    |
| 5/26/2019   | net  | pc          |                200,000.00  |
| 5/26/2019   | net  | NULL        |                200,000.00  |
| 5/26/2019   | NULL | NULL        |                200,000.00  |
| NULL        | NULL | NULL        |                200,000.00  |
+-------------+------+-------------+----------------------------+

1 个答案:

答案 0 :(得分:2)

汇总正常工作。

您在联接中的site上缺少联接条件:

LEFT JOIN cte
ON base.target_date = cte.target_date 
            AND base.device = cte.device

这导致值重复。