Sql server 在sql中按时间间隔分组数据
我有一张如下的桌子Sql server 在sql中按时间间隔分组数据,sql-server,Sql Server,我有一张如下的桌子 order no orderdate 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
order no orderdate
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
我想按时间间隔(如00-01、01-02……22-23、23-24)对这些值进行分组(不包括日期值)
如何仅按时间将查询写入此组
我想得到24行(24个间隔)的订单计数,这里有一个简单的解决方案,你可以用一天中的小时来计算
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
如果存在dbo.tOrders,则删除表;
创建表dbo.tOrders(
OrderNo varchar(50)
,OrderDate日期时间
);
插入dbo.tOrders(订单号、订单日期)
数值('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')
选择
案例
当datepart(小时,t.OrderDate)>=0且datepart(小时,t.OrderDate)<1时,则为“00-01”
当datepart(小时,t.OrderDate)>=1且datepart(小时,t.OrderDate)<2时,则为“01-02”
当datepart(小时,t.OrderDate)>=2且datepart(小时,t.OrderDate)<3时,则为“02-03”
当datepart(小时,t.OrderDate)>=3且datepart(小时,t.OrderDate)<4时,则为“03-04”
当datepart(小时,t.OrderDate)>=4且datepart(小时,t.OrderDate)<5时,则为“04-05”
当datepart(小时,t.OrderDate)>=5且datepart(小时,t.OrderDate)<6时,则为“05-06”
当datepart(小时,t.OrderDate)>=6且datepart(小时,t.OrderDate)<7时,则为“06-07”
当datepart(小时,t.OrderDate)>=7且datepart(小时,t.OrderDate)<8时,则为“07-08”
当datepart(小时,t.OrderDate)>=8且datepart(小时,t.OrderDate)<9时,则为“08-09”
当datepart(小时,t.OrderDate)>=9且datepart(小时,t.OrderDate)<10时,则为“09-10”
当datepart(小时,t.OrderDate)>=10且datepart(小时,t.OrderDate)<11时,则为“10-11”
当datepart(小时,t.OrderDate)>=11且datepart(小时,t.OrderDate)<12时,则为“11-12”
当datepart(小时,t.OrderDate)>=12且datepart(小时,t.OrderDate)<13时,则为“12-13”
当datepart(小时,t.OrderDate)>=13且datepart(小时,t.OrderDate)<14时,则为“13-14”
当datepart(小时,t.OrderDate)>=14且datepart(小时,t.OrderDate)<15时,则为“14-15”
当datepart(小时,t.OrderDate)>=15且datepart(小时,t.OrderDate)<16时,则为“15-16”
当datepart(小时,t.OrderDate)>=16且datepart(小时,t.OrderDate)<17时,则为“16-17”
当datepart(小时,t.OrderDate)>=17且datepart(小时,t.OrderDate)<18时,则为“17-18”
当datepart(小时,t.OrderDate)>=18且datepart(小时,t.OrderDate)<19时,则为“18-19”
当datepart(小时,t.OrderDate)>=19且datepart(小时,t.OrderDate)<20时,则为“19-20”
当datepart(小时,t.OrderDate)>=20且datepart(小时,t.OrderDate)<21时,则为“20-21”
当datepart(小时,t.OrderDate)>=21且datepart(小时,t.OrderDate)<22时,则为“21-22”
当datepart(小时,t.OrderDate)>=22且datepart(小时,t.OrderDate)<23时,则为“22-23”
当datepart(小时,t.OrderDate)>=23时,则为“23-00”
作为一天的时间结束
,将(t.OrderNo)计数为OrderCount
来自dbo.tOrders t
分组
案例
当datepart(小时,t.OrderDate)>=0且datepart(小时,t.OrderDate)<1时,则为“00-01”
当datepart(小时,t.OrderDate)>=1且datepart(小时,t.OrderDate)<2时,则为“01-02”
当datepart(小时,t.OrderDate)>=2且datepart(小时,t.OrderDate)<3时,则为“02-03”
当datepart(小时,t.OrderDate)>=3且datepart(小时,t.OrderDate)<4时,则为“03-04”
当datepart(小时,t.OrderDate)>=4且datepart(小时,t.OrderDate)<5时,则为“04-05”
当datepart(小时,t.OrderDate)>=5且datepart(小时,t.OrderDate)<6时,则为“05-06”
当datepart(小时,t.OrderDate)>=6且datepart(小时,t.OrderDate)<7时,则为“06-07”
当datepart(小时,t.OrderDate)>=7且datepart(小时,t.OrderDate)<8时,则为“07-08”
当datepart(小时,t.OrderDate)>=8且datepart(小时,t.OrderDate)<9时,则为“08-09”
当datepart(小时,t.OrderDate)>=9且datepart(小时,t.OrderDate)<10时,则为“09-10”
当datepart(小时,t.OrderDate)>=10且datepart(小时,t.OrderDate)<11时,则为“10-11”
当datepart(小时,t.OrderDate)>=11且datepart(小时,t.OrderDate)<12时,则为“11-12”
当datepart(小时,t.OrderDate)>=12且datepart(小时,t.OrderDate)<13时,则为“12-13”
当datepart(小时,t.OrderDate)>=13且datepart(小时,t.OrderDate)<14时,则为“13-14”
当datepart(小时,t.OrderDate)>=14且datepart(小时,t.OrderDate)<15时,则为“14-15”
当datepart(小时,t.OrderDate)>=15且datepart(小时,t.OrderDate)<16时,则为“15-16”
当datepart(小时,t.OrderDate)>=16且datepart(小时,t.OrderDate)<17时,则为“16-17”
当datepart(小时,t.OrderDate)>=17且datepart(小时,t.OrderDate)<18时,则为“17-18”
当datepart(小时,t.OrderDate)>=18且datepart(小时,t.OrderDate)<19时,则为“18-19”
当datepart(小时,t.OrderDate)>=19且datepart(小时,t.OrderDate)<20时,则为“19-20”
当datepart(小时,t.OrderDate)>=20且datepart(小时,t.OrderDate)<21时,则为“20-21”
当datepart(小时,t.OrderDate)>=21且datepart(小时,t.OrderDate)<22时,则为“21-22”
当datepart(小时,t.OrderDate)>=22且datepart(小时,t.OrderDate)<23时,则为“22-23”
当datepart(小时,t.OrderDate)>=23时,则为“23-00”
结束
您可以使用按日期分组部分(小时、订单日期)
查询:
SELECT DATEPART(HOUR, orderdate) AS HOUR,
COUNT(*) AS ORDERS_PER_HOUR
FROM YourTable
WHERE CAST(orderdate AS DATE) = @someDate
GROUP BY DATEPART(HOUR, orderdate)
请注意,如果在orderdate
中不是每小时出现一次,则上述查询将给出少于24个结果
如果需要表示所有小时,则可以使用递归公共表表达式(CTE)首先生成所有小时(0-23),然后
;WITH hours AS
(
SELECT 0 AS h
UNION ALL
SELECT h + 1 FROM hours WHERE h < 23
)
SELECT hours.h AS HOUR,
COUNT(YourTable.[order no]) AS ORDERS_PER_HOUR
FROM hours
LEFT JOIN YourTable ON DATEPART(HOUR, orderdate) = hours.h
WHERE CAST(orderdate AS DATE) = @someDate
GROUP BY hours.h
SELECT CAST(X AS VARCHAR(2)) +'-'+CAST(X+1 AS VARCHAR(2)) AS HOUR_GROUP, COUNT(*) AS RC
FROM
(SELECT DATEPART(hh,ORD_DATE) X FROM ORD)A
GROUP BY X
;
SELECT CAST(DATEPART(hh,ORD_DATE) AS VARCHAR(2)) +'-'+CAST(DATEPART(hh,ORD_DATE) +1 AS VARCHAR(2)) AS HOUR_GROUP, COUNT(*) AS RC
FROM ORD
GROUP BY DATEPART(hh,ORD_DATE)
;
INSERT INTO ORD VALUES (1,'2012-01-08 00:00');
INSERT INTO ORD VALUES (2,'2012-01-08 00:10');
INSERT INTO ORD VALUES (3,'2012-01-08 00:15');
INSERT INTO ORD VALUES (4,'2012-01-08 00:30');
INSERT INTO ORD VALUES (9,'2012-01-08 01:30');
INSERT INTO ORD VALUES (5,'2012-01-08 10:00');
INSERT INTO ORD VALUES(7,'2012-01-08 15:00');
INSERT INTO ORD VALUES (8,'2012-02-08 20:30');
HOUR_GROUP RC
0-1 4
1-2 1
10-11 1
15-16 1
20-21 1
SELECT RIGHT('0'+CAST(Z AS VARCHAR(2)),2)+'-'+RIGHT('0'+CAST(Z+1 AS VARCHAR(2)),2) AS HOUR_GR, COALESCE(RC,0) AS RC
FROM (
SELECT -1+ROW_NUMBER() OVER (ORDER BY A1.Y) AS Z
FROM (SELECT 1 AS Y UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5) A1
CROSS JOIN (SELECT 1 AS Y UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5) A2
) B
LEFT JOIN (SELECT X, CAST(X AS VARCHAR(2)) +'-'+CAST(X +1 AS VARCHAR(2)) AS HOUR_GROUP, COUNT(*) AS RC
FROM (SELECT DATEPART(hh,ORD_DATE) X FROM ORD) A
GROUP BY X
) C ON B.Z= C.X
WHERE B.Z<24
HOUR_GR RC
00-01 4
01-02 1
02-03 0
03-04 0
04-05 0
05-06 0
06-07 0
07-08 0
08-09 0
09-10 0
10-11 1
11-12 0
12-13 0
13-14 0
14-15 0
15-16 1
16-17 0
17-18 0
18-19 0
19-20 0
20-21 1
21-22 0
22-23 0
23-24 0
declare @hours table
(
hourpart datetime
)
INSERT INTO @hours VALUES
('00:00:00'),
('01:00:00'),
('02:00:00'),
('03:00:00'),
('04:00:00'),
('05:00:00'),
('06:00:00'),
('07:00:00'),
('08:00:00'),
('09:00:00'),
('10:00:00'),
('11:00:00'),
('12:00:00'),
('13:00:00'),
('14:00:00'),
('15:00:00'),
('16:00:00'),
('17:00:00'),
('18:00:00'),
('19:00:00'),
('20:00:00'),
('21:00:00'),
('22:00:00'),
('23:00:00')
declare @orders table
(
orderNo int,
orderDate datetime)
INSERT INTO @orders 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'),
(11,'2012-08-01 20:25')
SELECT convert(varchar(8), h.hourpart, 108), COUNT(*) FROM @orders o
INNER JOIN @hours h
ON DATEPART(hour, o.orderdate) = DATEPART(hour, h.hourpart)
GROUP BY h.hourpart
ORDER BY h.hourpart
00:00:00 4
10:00:00 2
14:00:00 1
20:00:00 3
23:00:00 1