有一表A。。里面有字段: 时间 号码 。。等
比如:时间 号码 。。 。。 。。
2010-08-30 17:28:44.000 15188550386 .. ..
2010-08-30 17:28:44.000 15214151731
2010-08-30 17:28:44.000 15963803516
2010-08-30 17:28:45.000 。。
2010-08-30 17:28:45.000 ..
2010-08-30 17:28:45.000 ..
2010-08-30 17:28:45.000 ..
.....
...
现要求 整个表A里面。。每5分钟为一个时间段,分别的人数和次数。
结果类似于:时间段(每5分钟) 人数 次数
00-05 .. ..
05-10 .. ..
。。
比如:时间 号码 。。 。。 。。
2010-08-30 17:28:44.000 15188550386 .. ..
2010-08-30 17:28:44.000 15214151731
2010-08-30 17:28:44.000 15963803516
2010-08-30 17:28:45.000 。。
2010-08-30 17:28:45.000 ..
2010-08-30 17:28:45.000 ..
2010-08-30 17:28:45.000 ..
.....
...
现要求 整个表A里面。。每5分钟为一个时间段,分别的人数和次数。
结果类似于:时间段(每5分钟) 人数 次数
00-05 .. ..
05-10 .. ..
。。
--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(logtime datetime, c2 varchar(8), c3 varchar(20))
insert into #
select '2009-05-25 15:04:34.143', 'WAP', 13703008260 union all
select '2009-05-25 16:37:27.547', 'WAP', 15017646007 union all
select '2009-05-25 10:36:34.027', 'WAP', 13726015199 union all
select '2009-05-25 05:46:28.203', 'WAP', 13790441485 union all
select '2009-05-25 00:57:53.863', 'WAP', 15878633245 union all
select '2009-04-26 13:15:22.913', 'WAP', 13723748918 union all
select '2009-04-26 15:25:51.767', 'WAP', 15813434631 union all
select '2009-04-26 15:26:37.040', 'WAP', 15813434631select [min-min] = right('0'+ltrim(number*5),2) + '-' + right('0'+ltrim((number+1)*5-1),2), a.* from
(select min5=datepart(mi,logtime)/5, 人数=count(distinct c3), 次数=count(1) from # group by datepart(mi,logtime)/5) a
inner join
master..spt_values b on b.type = 'P' and b.number = a.min5/*
min-min min5 人数 次数
--------- ----------- ----------- -----------
00-04 0 1 1
15-19 3 1 1
25-29 5 1 2
35-39 7 2 2
45-49 9 1 1
55-59 11 1 1
*/
SELECT convert(char(15),logtime,120) + case when right(convert(char(16),logtime,120),1) between 0 and 4 then '0'
else '5' end as dd这么写都可以搞定犯糊涂啦。。