---------------------------------------Author : liangCK 梁爱兰--Comment: 小梁 爱 兰儿--Date : 2010-01-02 16:47:10---------------------------------------> 生成测试数据: #tbCREATETABLE#tb(列名1varchar(12),时间datetime)INSERTINTO#tbSELECT'03174190188','2009-11-01 07:17:39.217'UNIONALLSELECT'015224486575','2009-11-01 08:01:17.153'UNIONALLSELECT'013593006926','2009-11-12 08:04:46.560'UNIONALLSELECT'013599584239','2009-11-22 08:53:27.763'UNIONALLSELECT'013911693526','2009-11-23 08:53:51.683'UNIONALLSELECT'013846472440','2009-11-23 08:54:57.233'UNIONALLSELECT'013990353697','2009-11-24 08:55:25.077'UNIONALLSELECT'013990353697','2009-11-25 08:56:01.327'UNIONALLSELECT'013945594843','2009-11-26 08:57:02.233'UNIONALLSELECT'013990353697','2009-11-27 08:57:29.700'UNIONALLSELECT'013916597421','2009-11-28 08:59:49.390'UNIONALLSELECT'03916995857','2009-11-29 09:11:05.607'UNIONALLSELECT'015097712001','2009-11-30 09:13:50.293'--SQL查询如下:DECLARE@minDatedatetime,@maxDatedatetime;SELECT@minDate='2009-11-1',@maxDate='2009-12-01';selectconvert(char(10),dateadd(d,number,@minDate),120),sum(casewhenconvert(char(8),时间,108)between'00:00'and'01:00'then1else0end)as'00:00~01:00',sum(casewhenconvert(char(8),时间,108)between'01:00'and'02:00'then1else0end)as'01:00~02:00',sum(casewhenconvert(char(8),时间,108)between'02:00'and'03:00'then1else0end)as'02:00~03:00',sum(casewhenconvert(char(8),时间,108)between'03:00'and'04:00'then1else0end)as'03:00~04:00',sum(casewhenconvert(char(8),时间,108)between'04:00'and'05:00'then1else0end)as'04:00~05:00',sum(casewhenconvert(char(8),时间,108)between'05:00'and'06:00'then1else0end)as'05:00~06:00',sum(casewhenconvert(char(8),时间,108)between'06:00'and'07:00'then1else0end)as'06:00~07:00',sum(casewhenconvert(char(8),时间,108)between'07:00'and'08:00'then1else0end)as'07:00~08:00',sum(casewhenconvert(char(8),时间,108)between'08:00'and'09:00'then1else0end)as'08:00~09:00',count(a.列名1)as'sum'from#tb arightjoinmaster..spt_values bondatediff(d,时间,dateadd(d,number,@minDate))=0wheredateadd(d,number,@minDate)<=@maxDateandb.type='p'andb.number>=0groupbyconvert(char(10),dateadd(d,number,@minDate),120)orderby1droptable#tb(13行受影响)00:00~01:0001:00~02:0002:00~03:0003:00~04:0004:00~05:0005:00~06:0006:00~07:0007:00~08:0008:00~09:00sum---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------2009-11-0100000001122009-11-0200000000002009-11-0300000000002009-11-0400000000002009-11-0500000000002009-11-0600000000002009-11-0700000000002009-11-0800000000002009-11-0900000000002009-11-1000000000002009-11-1100000000002009-11-1200000000112009-11-1300000000002009-11-1400000000002009-11-1500000000002009-11-1600000000002009-11-1700000000002009-11-1800000000002009-11-1900000000002009-11-2000000000002009-11-2100000000002009-11-2200000000112009-11-2300000000222009-11-2400000000112009-11-2500000000112009-11-2600000000112009-11-2700000000112009-11-2800000000112009-11-2900000000012009-11-3000000000012009-12-010000000000警告: 聚合或其他SET操作消除了空值。(31行受影响)
---------------------------------------Author : liangCK 梁爱兰--Comment: 小梁 爱 兰儿--Date : 2010-01-02 16:47:10---------------------------------------> 生成测试数据: #tbCREATETABLE#tb(列名1varchar(12),时间datetime)INSERTINTO#tbSELECT'03174190188','2009-11-01 07:17:39.217'UNIONALLSELECT'015224486575','2009-11-01 08:01:17.153'UNIONALLSELECT'013593006926','2009-11-12 08:04:46.560'UNIONALLSELECT'013599584239','2009-11-22 08:53:27.763'UNIONALLSELECT'013911693526','2009-11-23 08:53:51.683'UNIONALLSELECT'013846472440','2009-11-23 08:54:57.233'UNIONALLSELECT'013990353697','2009-11-24 08:55:25.077'UNIONALLSELECT'013990353697','2009-11-25 08:56:01.327'UNIONALLSELECT'013945594843','2009-11-26 08:57:02.233'UNIONALLSELECT'013990353697','2009-11-27 08:57:29.700'UNIONALLSELECT'013916597421','2009-11-28 08:59:49.390'UNIONALLSELECT'03916995857','2009-11-29 09:11:05.607'UNIONALLSELECT'015097712001','2009-11-30 09:13:50.293'--SQL查询如下:DECLARE@minDatedatetime,@maxDatedatetime;SELECT@minDate='2009-11-1',@maxDate='2009-12-01';selectisnull(convert(char(10),dateadd(d,number,@minDate),120),'sum'),sum(casewhenconvert(char(8),时间,108)between'00:00'and'01:00'then1else0end)as'00:00~01:00',sum(casewhenconvert(char(8),时间,108)between'01:00'and'02:00'then1else0end)as'01:00~02:00',sum(casewhenconvert(char(8),时间,108)between'02:00'and'03:00'then1else0end)as'02:00~03:00',sum(casewhenconvert(char(8),时间,108)between'03:00'and'04:00'then1else0end)as'03:00~04:00',sum(casewhenconvert(char(8),时间,108)between'04:00'and'05:00'then1else0end)as'04:00~05:00',sum(casewhenconvert(char(8),时间,108)between'05:00'and'06:00'then1else0end)as'05:00~06:00',sum(casewhenconvert(char(8),时间,108)between'06:00'and'07:00'then1else0end)as'06:00~07:00',sum(casewhenconvert(char(8),时间,108)between'07:00'and'08:00'then1else0end)as'07:00~08:00',sum(casewhenconvert(char(8),时间,108)between'08:00'and'09:00'then1else0end)as'08:00~09:00',count(a.列名1)as'sum'from#tb arightjoinmaster..spt_values bondatediff(d,时间,dateadd(d,number,@minDate))=0wheredateadd(d,number,@minDate)<=@maxDateandb.type='p'andb.number>=0groupbyconvert(char(10),dateadd(d,number,@minDate),120)withrolluporderby1droptable#tb00:00~01:0001:00~02:0002:00~03:0003:00~04:0004:00~05:0005:00~06:0006:00~07:0007:00~08:0008:00~09:00sum---------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------2009-11-0100000001122009-11-0200000000002009-11-0300000000002009-11-0400000000002009-11-050000000000......2009-11-2800000000112009-11-2900000000012009-11-3000000000012009-12-010000000000sum000000011013(32行受影响)