的数量在这里您区分一天的时间一个简单的解决方案...
drop table if exists dbo.tOrders;
create table dbo.tOrders (
OrderNo varchar(50)
, OrderDate datetime
);
insert into dbo.tOrders (OrderNo, OrderDate)
values ('01', '2012-08-01 00:00')
, ('02', '2012-08-01 00:10')
, ('03', '2012-08-01 00:15')
, ('04', '2012-08-01 00:30')
, ('05', '2012-08-01 10:00')
, ('06', '2012-08-01 10:35')
, ('07', '2012-08-01 14:00')
, ('08', '2012-08-02 20:30')
, ('09', '2012-08-02 20:35')
, ('10', '2012-08-02 23:00')
select
case
when datepart(hour, t.OrderDate) >= 0 and datepart(hour, t.OrderDate) < 1 then '00-01'
when datepart(hour, t.OrderDate) >= 1 and datepart(hour, t.OrderDate) < 2 then '01-02'
when datepart(hour, t.OrderDate) >= 2 and datepart(hour, t.OrderDate) < 3 then '02-03'
when datepart(hour, t.OrderDate) >= 3 and datepart(hour, t.OrderDate) < 4 then '03-04'
when datepart(hour, t.OrderDate) >= 4 and datepart(hour, t.OrderDate) < 5 then '04-05'
when datepart(hour, t.OrderDate) >= 5 and datepart(hour, t.OrderDate) < 6 then '05-06'
when datepart(hour, t.OrderDate) >= 6 and datepart(hour, t.OrderDate) < 7 then '06-07'
when datepart(hour, t.OrderDate) >= 7 and datepart(hour, t.OrderDate) < 8 then '07-08'
when datepart(hour, t.OrderDate) >= 8 and datepart(hour, t.OrderDate) < 9 then '08-09'
when datepart(hour, t.OrderDate) >= 9 and datepart(hour, t.OrderDate) < 10 then '09-10'
when datepart(hour, t.OrderDate) >= 10 and datepart(hour, t.OrderDate) < 11 then '10-11'
when datepart(hour, t.OrderDate) >= 11 and datepart(hour, t.OrderDate) < 12 then '11-12'
when datepart(hour, t.OrderDate) >= 12 and datepart(hour, t.OrderDate) < 13 then '12-13'
when datepart(hour, t.OrderDate) >= 13 and datepart(hour, t.OrderDate) < 14 then '13-14'
when datepart(hour, t.OrderDate) >= 14 and datepart(hour, t.OrderDate) < 15 then '14-15'
when datepart(hour, t.OrderDate) >= 15 and datepart(hour, t.OrderDate) < 16 then '15-16'
when datepart(hour, t.OrderDate) >= 16 and datepart(hour, t.OrderDate) < 17 then '16-17'
when datepart(hour, t.OrderDate) >= 17 and datepart(hour, t.OrderDate) < 18 then '17-18'
when datepart(hour, t.OrderDate) >= 18 and datepart(hour, t.OrderDate) < 19 then '18-19'
when datepart(hour, t.OrderDate) >= 19 and datepart(hour, t.OrderDate) < 20 then '19-20'
when datepart(hour, t.OrderDate) >= 20 and datepart(hour, t.OrderDate) < 21 then '20-21'
when datepart(hour, t.OrderDate) >= 21 and datepart(hour, t.OrderDate) < 22 then '21-22'
when datepart(hour, t.OrderDate) >= 22 and datepart(hour, t.OrderDate) < 23 then '22-23'
when datepart(hour, t.OrderDate) >= 23 then '23-00'
end as TimeOfDay
, count(t.OrderNo) as OrderCount
from dbo.tOrders t
group by
case
when datepart(hour, t.OrderDate) >= 0 and datepart(hour, t.OrderDate) < 1 then '00-01'
when datepart(hour, t.OrderDate) >= 1 and datepart(hour, t.OrderDate) < 2 then '01-02'
when datepart(hour, t.OrderDate) >= 2 and datepart(hour, t.OrderDate) < 3 then '02-03'
when datepart(hour, t.OrderDate) >= 3 and datepart(hour, t.OrderDate) < 4 then '03-04'
when datepart(hour, t.OrderDate) >= 4 and datepart(hour, t.OrderDate) < 5 then '04-05'
when datepart(hour, t.OrderDate) >= 5 and datepart(hour, t.OrderDate) < 6 then '05-06'
when datepart(hour, t.OrderDate) >= 6 and datepart(hour, t.OrderDate) < 7 then '06-07'
when datepart(hour, t.OrderDate) >= 7 and datepart(hour, t.OrderDate) < 8 then '07-08'
when datepart(hour, t.OrderDate) >= 8 and datepart(hour, t.OrderDate) < 9 then '08-09'
when datepart(hour, t.OrderDate) >= 9 and datepart(hour, t.OrderDate) < 10 then '09-10'
when datepart(hour, t.OrderDate) >= 10 and datepart(hour, t.OrderDate) < 11 then '10-11'
when datepart(hour, t.OrderDate) >= 11 and datepart(hour, t.OrderDate) < 12 then '11-12'
when datepart(hour, t.OrderDate) >= 12 and datepart(hour, t.OrderDate) < 13 then '12-13'
when datepart(hour, t.OrderDate) >= 13 and datepart(hour, t.OrderDate) < 14 then '13-14'
when datepart(hour, t.OrderDate) >= 14 and datepart(hour, t.OrderDate) < 15 then '14-15'
when datepart(hour, t.OrderDate) >= 15 and datepart(hour, t.OrderDate) < 16 then '15-16'
when datepart(hour, t.OrderDate) >= 16 and datepart(hour, t.OrderDate) < 17 then '16-17'
when datepart(hour, t.OrderDate) >= 17 and datepart(hour, t.OrderDate) < 18 then '17-18'
when datepart(hour, t.OrderDate) >= 18 and datepart(hour, t.OrderDate) < 19 then '18-19'
when datepart(hour, t.OrderDate) >= 19 and datepart(hour, t.OrderDate) < 20 then '19-20'
when datepart(hour, t.OrderDate) >= 20 and datepart(hour, t.OrderDate) < 21 then '20-21'
when datepart(hour, t.OrderDate) >= 21 and datepart(hour, t.OrderDate) < 22 then '21-22'
when datepart(hour, t.OrderDate) >= 22 and datepart(hour, t.OrderDate) < 23 then '22-23'
when datepart(hour, t.OrderDate) >= 23 then '23-00'
end
可以'组时CONDITION1 THEN 1 BY CASE WHEN 2 THEN ... CONDITION2'。 – RealCheeseLord
'我想获得24行(24间隔)的订单数'!!,你能否以表格格式提供我们想要的输出? –