Data Lake • Write your own SQL

Add up by group

How much did each region sell?

Try this

  • Run this on my data lake: SELECT region, COUNT(*) AS orders, SUM(amount) AS revenue FROM samples.shop_orders GROUP BY region ORDER BY revenue DESC

In plain words

What is it?

Instead of reading thousands of orders, you get one line for each region.

How does it work?

GROUP BY puts rows with the same region together. COUNT counts them and SUM adds up the money. ORDER BY puts the biggest first.

Good to know

Reading never changes your data. Only 10 rows are shown unless you ask for all of them.

Then try

Only the big regions

  • Run this on my data lake: SELECT region, COUNT(*) AS orders, SUM(amount) AS revenue FROM samples.shop_orders GROUP BY region HAVING SUM(amount) > 25000 ORDER BY revenue DESC

Add a grand total line

  • Run this on my data lake: SELECT region, SUM(amount) AS revenue FROM samples.shop_orders GROUP BY ROLLUP(region) ORDER BY region