Every advanced user of SQL should be familiar with GROUP BY and HAVING clauses. But are you also aware of CUBE, ROLLUP, and GROUPING SETS? If not, this chapter might be worth reading for you.
Introducing grouping sets
Loading some sample data
To make this chapter a pleasant experience for you, I have compiled some sample data, which has been taken from the BP energy report: http://www.bp.com/en/global/corporate/energy-economics/statistical-review-of-world-energy.html.
Here is the data structure that will be used:
test=# CREATE TABLE t_oil (
region text,
country text,
year int,
production int,
consumption int
);
CREATE TABLE
The test data can be downloaded from our website using curl directly:
test=# COPY t_oil...