按小时分组统计
有张表
SELECT AdLogID, AdLogIpId, AdLogAddtime, KeywordID
FROM UctoyAdLogs
WHERE (KeywordID = 195) AND (LogID IN (33, 34, 35, 36))
AND (convert(varchar(10),AdLogAddtime,120) BETWEEN
'2007-04-25 ' AND '2007-04-26 ')
根据条件取出来的是:
AdLogID AdLogIpId AdLogAddtime
1086 648 2007-4-25 11:34:01
1089 648 2007-4-25 10:34:02
1390 15 2007-4-26 9:49:03
1392 10 2007-4-25 9:49:03
1396 15 2007-4-25 12:49:03
1397 13 2007-4-25 9:49:03
1485 15 2007-3-25 13:53:15
1487 15 2007-3-26 9:53:16
1491 12 2007-4-26 14:53:16
1492 9 2007-4-25 9:53:16
1499 15 2007-4-26 9:54:57
1501 11 2007-4-25 14:54:57
1505 15 2007-4-25 9:54:58
1507 15 2007-4-25 9:54:58
现在想按小时 达到如下统计效果
小时段 count ipcount
0:00-1:00 0 0
1:00-2:00 0 1
2:00-3:00 0 0
3:00-4:00 1 0
4:00-5:00 3 0
5:00-6:00 4 0
6:00-7:00 5 0
7:00-8:00 6 0
8:00-9:00 0 0
9:00-10:00 2 1
......
22:00-23:00 0 0
23:00-24:00 0 0
count 是统计该时间段内的记录数 ipcount是统计该时间段内的AdLogIpId数量
请问高手 该怎么实现??
------解决方案--------------------select
a.小时段,
isnull(count(b.AdLogID) ,0),
isnull(count(distinct b.AdLogIpId),0)
from
(select 0 as hh, '0:00-1:00 ' as 小时段 union
select 1, '1:00-2:00 ' union
...
select 23, '23:00-24:00 ') a
left join
UctoyAdLogs b
on
a.hh=datepart(hh,b.AdLogAddtime)
and
...
group by
a.小时段
------解决方案--------------------up