1. 程式人生 > >sql server 按年、季度、月份、周統計訂單銷量

sql server 按年、季度、月份、周統計訂單銷量



----------------------------------------------------------------
--按周統計
SELECT TOP 10 
 DATENAME(year,AddDate) 年,
 DATENAME(week,AddDate) 周,
 COUNT(1) 單量,
 SUM(total) 總金額,
 AVG(total) 客單價
FROM orderinfo t(NOLOCK) 
WHERE AddDate BETWEEN '2013-1-1' AND '2014-1-1'
GROUP BY DATENAME(year,AddDate),DATENAME(week,AddDate)
ORDER BY CONVERT(INT,DATENAME(week,AddDate));


----------------------------------------------------------------
--按月統計
SELECT TOP 10 
 DATENAME(year,AddDate) 年,
 DATENAME(month,AddDate) 月,
 COUNT(1) 單量,
 SUM(total) 總金額,
 AVG(total) 客單價
FROM orderinfo t(NOLOCK) 
WHERE AddDate BETWEEN '2013-1-1' AND '2014-1-1'
GROUP BY DATENAME(year,AddDate),DATENAME(month,AddDate)
ORDER BY CONVERT(INT,DATENAME(month,AddDate));


----------------------------------------------------------------
--按季度統計
SELECT TOP 10 
 DATENAME(year,AddDate) 年,
 DATENAME(quarter,AddDate) 季度,
 COUNT(1) 單量,
 SUM(total) 總金額,
 AVG(total) 客單價
FROM orderinfo t(NOLOCK) 
WHERE AddDate BETWEEN '2013-1-1' AND '2014-1-1'
GROUP BY DATENAME(year,AddDate),DATENAME(quarter,AddDate)
ORDER BY CONVERT(INT,DATENAME(quarter,AddDate));


----------------------------------------------------------------
--按年統計
SELECT TOP 10 
 DATENAME(year,AddDate) 年,
 COUNT(1) 單量,
 SUM(total) 總金額,
 AVG(total) 客單價
FROM orderinfo t(NOLOCK) 
WHERE AddDate BETWEEN '2012-1-1' AND '2014-1-1'
GROUP BY DATENAME(year,AddDate)
ORDER BY CONVERT(INT,DATENAME(year,AddDate));