简体   繁体   English

如何在查询结果中添加汇总行?

[英]How to add an aggregated row to the result of the query?

I need help with an SQL query. 我需要有关SQL查询的帮助。

Here's a query to create a test table: 这是创建测试表的查询:

CREATE TABLE test(year_field nvarchar(max), month_field nvarchar(max), category nvarchar(max), items nvarchar(max), code nvarchar(max))
INSERT INTO test(year_field, month_field, category, items, code)
VALUES ('2019','01','C','120','M'),
       ('2019','01','C','20','M'),
       ('2019','01','C','140','M'),
       ('2019','01','C','120','M'),
       ('2019','01','C','80','M'),
       ('2019','01','C','10','M'),
       ('2019','01','C','100','M'),
       ('2019','01','C','210','M'),
       ('2019','01','C','70','M'),
       ('2019','01','C','310',M'),
       ('2019','01','C','10','M'),
       ('2019','01','C','10','O'),
       ('2019','01','C','10','O'),
       ('2019','01','C','100','O'),
       ('2019','01','C','210','O'),
       ('2019','01','C','10','O'),
       ('2019','01','C','70','O'),
       ('2019','01','C','90','O'),
       ('2019','01','C','140','O'),
       ('2019','01','C','70','O'),
       ('2019','01','C','150','O'),
       ('2019','01','C','260','O'),
       ('2019','01','P','10','M'),
       ('2019','01','P','10','M'),
       ('2019','01','P','170','M'),
       ('2019','01','P','70','M'),
       ('2019','01','P','120','M'),
       ('2019','01','P','30','M'),
       ('2019','01','P','10','M'),
       ('2019','01','P','100','M'),
       ('2019','01','P','20','O'),
       ('2019','01','P','30','O')

I have written a query that sums up items_quantity fields values by category and code . 我编写了一个查询,该查询按类别代码总结了items_quantity字段的值。 Here it is: 这里是:

SELECT category, code, SUM(items_quantity) FROM ( 

    SELECT year_field, month_field, code, category, 
           SUM((CAST(items as INT)) / 10) items_quantity

    FROM test 
    WHERE (category = 'C' OR category = 'P') 
    GROUP BY year_field, month_field, code, category
) a

WHERE year_field = 2019 AND month_field = 1     
AND (code = 'O' OR code = 'M')
GROUP BY code, category

which gets me the following result: 这给我以下结果:

在此处输入图片说明

Now I also need to include one more row to the result, which would be the SUM aggregated by the code field, something like this: 现在,我还需要在结果中再增加一行,这将是由代码字段聚合的SUM,如下所示:

在此处输入图片说明

It would have NULL instead of the category because here we sum up all the categories of one code. 它将具有NULL而不是类别,因为这里我们总结了一个代码的所有类别。

Is it possible to do something like this? 有可能做这样的事情吗?

Try using GROUP BY with ROLLUP 尝试将GROUP BY与ROLLUP一起使用

SELECT category, code, SUM(items_quantity) FROM ( 

    SELECT year_field, month_field, code, category, 
           SUM((CAST(items as INT)) / 10) items_quantity

    FROM test 
    WHERE (category = 'C' OR category = 'P') 
    GROUP BY year_field, month_field, code, category
) a

WHERE year_field = 2019 AND month_field = 1     
AND (code = 'O' OR code = 'M')
GROUP BY ROLLUP(code, category)

You can use union all to combine two result sets 您可以使用union all合并两个结果集

with cte as 
(
    SELECT year_field, month_field, code, category, 
           SUM((CAST(items as INT)) / 10) items_quantity
    FROM test 
    WHERE (category = 'C' OR category = 'P') 
    GROUP BY year_field, month_field, code, category
)

select SELECT category, code, SUM(items_quantity) FROM
cte WHERE year_field = 2019 AND month_field = 1 AND (code = 'O' OR code = 'M')
GROUP BY code, category
union all
select null, code,SUM(items_quantity) from cte where code = 'M'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM