couple of variants here, one using old style GROUP BY statements:
GROUP BY
( SELECT city, product_line, date_part('year', sale_date)::INT AS year, date_part('month', sale_date)::INT AS month, SUM(total) AS sales FROM supermarket GROUP BY 1, 2, 3, 4 UNION ALL SELECT city, product_line, date_part('year', sale_date)::INT, NULL, SUM(total) AS sales FROM supermarket GROUP BY 1, 2, 3 ) ORDER BY 1, 2, 3, 4;
and a version (after actually googling about GROUPING SETS) that probably does what you were expecting:
GROUPING SETS
SELECT city, product_line, date_part('year', sale_date)::INT AS year, date_part('month', sale_date)::INT AS month, SUM(total) AS sales FROM supermarket GROUP BY GROUPING SETS ( (1, 2, 3), (1, 2, 3, 4) ) ORDER BY 1, 2, 3, 4;
be interesting to see if you did anything different!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
couple of variants here, one using old style
GROUP BY
statements:and a version (after actually googling about
GROUPING SETS
) that probably does what you were expecting:be interesting to see if you did anything different!