撰写查询以获取大多数货件发送的星期几。例如,我们在星期五发送大部分货物。
SELECT
DATEPART(DW, [ShipDate]) AS 'Day',
COUNT(*) AS 'Number of shipments/per day'
FROM
[Purchasing].[PurchaseOrderHeader]
WHERE
[ShipDate] IS NOT NULL
GROUP BY
DATEPART(DW, [ShipDate])
ORDER BY
Day;
我得到了这个结果:
Day Number of shipments/per day
------------------------------------
1 693
2 164
3 232
4 728
5 577
6 842
7 776
答案 0 :(得分:2)
使用derived table
,case statement
和concat
尝试此操作,否则请使用其他concatenation method
select
concat ('We sent most shipments on ',
(case DD
when 1 then 'Sunday'
when 2 then 'Monday'
when 3 then 'Tueday'
when 4 then 'Wednesday'
when 5 then 'Thursday'
when 6 then 'Friday'
else
'Saturday'
end
)) result
from
(
SELECT top 1 DATEPART(DW, [ShipDate]) AS 'DD', COUNT(*) cnt
FROM [Purchasing].[PurchaseOrderHeader]
WHERE [ShipDate] IS NOT NULL
Group by DATEPART(DW, [ShipDate])
order by cnt desc
) x