Data Lake • Write your own SQL

Coming from another database

Why did my usual command fail?

Try this

  • Run this on my data lake: SELECT EXTRACT(YEAR FROM order_date) AS year, to_char(order_date, '%Y-%m') AS month, CURRENT_DATE - MAX(order_date) AS days_since, CAST(SUM(units) AS DOUBLE) / COUNT(*) AS units_per_order, string_agg(DISTINCT region, ', ') AS regions FROM samples.shop_orders GROUP BY 1, 2 ORDER BY 2 DESC LIMIT 3

In plain words

What is it?

Some words you know from other databases do not work here. Your command runs exactly as you wrote it.

How does it work?

Use EXTRACT(YEAR FROM d) not YEAR(d). Subtract dates, not DATEDIFF. Use d + INTERVAL '30 days' not DATE_ADD, LIMIT not TOP, string_agg not GROUP_CONCAT, and CASE not IF.

Good to know

Whole number divided by whole number drops the remainder: CAST one side to DOUBLE. EXPLAIN is not available.

Then try

Add days, fill blanks, label rows

  • Run this on my data lake: SELECT order_id, order_date + INTERVAL '30 days' AS due, COALESCE(region, 'unknown') AS region, CASE WHEN amount > 50 THEN 'big' ELSE 'small' END AS size FROM samples.shop_orders ORDER BY order_date DESC, order_id LIMIT 3