I need help with an SQL query.
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 . 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:
It would have NULL instead of the category because here we sum up all the categories of one code.
Is it possible to do something like this?
Try using GROUP BY with 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
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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.