我有一张销售表,想要获得订单的星期和一周中每天的订单数量。一切似乎都有效但无论我尝试什么'周'都是非标准的。我试过SET DATEFIRST 1;但仍然没有得到我想要的结果。
SELECT DATENAME(Weekday,orderdate) AS Weekday, COUNT(orderid) AS NumOrders
FROM Sales.Orders
GROUP BY DATENAME(Weekday,orderdate);
结果:
Weekday | NumOrders
1. Wednesday 25
2. Saturday 33
3. Monday 100
4. Sunday 115
5. Thursday 87
6. Tuesday 42
周五没有订单会导致输出没有按周开始订购吗?感谢。
答案 0 :(得分:3)
我认为你只需要一个ORDER BY
:
SELECT DATENAME(Weekday, orderdate) AS Weekday, COUNT(orderid) AS NumOrders
FROM Sales.Orders
GROUP BY DATENAME(Weekday, orderdate), DATEPART(Weekday, orderdate)
ORDER BY DATEPART(Weekday, orderdate);