sql - Overall summary with multiple GROUP BY -
lets have table called census following information:
country province city population ============================================== usa california sacramento 1234 usa california sanfran 4321 usa texas houston 1111 usa texas dallas 2222 canada ontario ottawa 3333 canada manitoba winnipeg 4444 i'm building report @ country/province level, gives me following:
select country, province, sum(population) census group country, province; country province sum(population) ======================================= usa california 5555 usa texas 3333 canada ontario 3333 canada manitoba 4444 i'm looking have "overall summary" row included on report, final result looks like:
country province sum(population) ======================================= usa california 5555 usa texas 3333 canada ontario 3333 canada manitoba 4444 total 16665 i'm acquainted rollups, can't seem find combination gets me i'm looking for. using group rollup(country, province) includes total value want, includes large number of values don't care about. true group rollup(country), province
how can go making "total" record?
i'm calculating union all , repeating 90% of first query different group by, because first query non-trivial, result slow , ugly code.
here's sql fiddle want play this: http://sqlfiddle.com/#!4/12ad9/5
this grouping sets expressions designed do:
select country, province, sum(population) census group grouping sets ( (country, province), -- first group country , province () -- (nothing), i.e. total grouping ); see sql-fiddle
Comments
Post a Comment