【小練習】SQL_DATE
阿新 • • 發佈:2019-02-06
Walmart
在哪一年的哪一個月在銅版紙上的消費最多?SELECT DATE_TRUNC('month', o.occurred_at) ord_date, SUM(o.gloss_amt_usd) tot_spent
FROM orders o
JOIN accounts a
ON a.id = o.account_id
WHERE a.name = 'Walmart'
GROUP BY 1
ORDER BY 2 DESC
LIMIT 1;
在 2016 年 5 月,Walmart 在銅版紙上的消費做多。或者下面這種寫法: SELECT DATE_PART('year', occurred_at) ord_year, DATE_PART('month', occurred_at) ord_month, SUM(o.gloss_amt_usd) gloss_spent
FROM orders o
JOIN accounts a
ON a.id = o.account_id
WHERE a.name = 'Walmart'
GROUP BY 1, 2
ORDER BY 3 DESC
LIMIT 1;