Mastering Date - Based Data Analysis with SQL
1. Aggregating Data over a Date Range
Let’s say your sales manager wants to know the average daily purchase amount over six months. You can use the following SQL code:
SELECT SUM(Cost)
/ DATEDIFF('2015-12-31', '2015-07-01')
AS AverageDailyCost
FROM allsales
WHERE DateBought BETWEEN '2015-07-01' AND '2015-12-31';
How it works
- Date Range Filtering : The
WHEREclause filters the data for the date range from July 1, 2015, to December 31, 2015. - Total Cost Calculation : The
SUM()function in theSELECTclause calculates
掌握SQL日期数据分析
超级会员免费看
订阅专栏 解锁全文
76

被折叠的 条评论
为什么被折叠?



