我希望按周和日分组数据。说我想找(Januray所有星期日的音量总和)+(2月所有星期日的音量总和)+ ...我做的就像是
select CONVERT(VARCHAR(10),date,111),
DATENAME(weekday,date) as Weekday,
DATEPART(month,date) as Month,
DATEPART(year,date) as year,
sum(volume)
from traffic_data_replica
group by CONVERT(VARCHAR(10),date,111),
DATENAME(weekday,date),
DATEPART(month,date),
DATEPART(year,date)
order by DATEPART(year,date),
CONVERT(VARCHAR(10),date,111),
DATEPART(month,date),
DATENAME(weekday,date) ;
但这不是我想要的理想结果:(
答案 0 :(得分:3)
您在组中包含实际日期(CONVERT(VARCHAR(10),date,111)
),因此日期是汇总级别。尝试这样的事情:
SELECT Year(date),
Month(date),
Datename(weekday, date) AS Weekday,
Sum(volume)
FROM traffic_data_replica
GROUP BY Year(date),
Month(date),
Datename(weekday, date)
ORDER BY Year(date),
Month(date),
Datename(weekday, date);
您可以使用datepart()
代替year()
和month()
。我发现后者更容易阅读和输入。