感觉有点难
select  * into #t  from
(
select 1 id, (cast ( '2006-06-11 05:52:12' as datetime)) dt, 1360065460758 as d
union select 2 , '2006-06-11 06:09:05', 1380138017292
union select 3 , '2006-06-11 06:25:13 ',97363849310
union select 4 , '2006-06-11 06:26:08 ',61763849310
union select 5 , '2006-06-11 06:34:59 ',60054190811
union select 6, '2006-06-11 06:44:04 ',1397305138553
union select 7, '2006-06-11 06:51:30 ',60063849310   
union select 8 , '2006-06-11 06:53:14 ',92713061620671
union select 9 , '2006-06-11 06:53:22 ',11713061620671
union select 10, '2006-06-11 06:58:34 ',32054196057
union select 11 , '2006-06-11 07:04:42 ',92752581156
) tselect count(*) from #t,(select top 1 dt from #t order by dt) t
group by datediff(mi, t.dt,#t.dt)/5            
----------- 
1
1
2
1
1
1
2
1
1(所影响的行数为 9 行)

解决方案 »

  1.   

    create table temp(id int,Dtime datetime,telephone varchar(20))insert into temp
    select 1, '2006-06-11 05:52:12', 1360065460758
    union select 2,'2006-06-11 06:09:05' ,1380138017292
    union select 3, '2006-06-11 06:25:13', 97363849310
    union select 4, '2006-06-11 06:26:08', 61763849310
    union select 5, '2006-06-11 06:34:59', 60054190811
    union select 6, '2006-06-11 06:44:04', 1397305138553
    union select 7, '2006-06-11 06:51:30', 60063849310   
    union select 8, '2006-06-11 06:53:14', 92713061620671
    union select 9, '2006-06-11 06:53:22', 11713061620671
    union select 10, '2006-06-11 06:58:34', 32054196057
    union select 11, '2006-06-11 07:04:42', 92752581156
    declare @mintime datetime
    select @mintime = MIN(DTIME) FROM TEMP
    ;
    with t as
    (
    select DATEDIFF(minute,@mintime,Dtime)/5 as times,DATEDIFF(minute,@mintime,Dtime)/5 as times2 from temp
    )select count(times) from t
    group by times2drop table temp--输出结果
    1
    1
    2
    1
    1
    1
    2
    1
    1
      

  2.   

    /*------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-17 22:01:29
    --  Version: Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
        Jul  9 2008 14:43:34 
        Copyright (c) 1988-2008 Microsoft Corporation
        Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)------------------------------------------------------------------*/
    --> 生成测试数据表:tbIF OBJECT_ID('[tb]') IS NOT NULL
        DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([id] INT,[time] DATETIME,[telephone] NVARCHAR(20))
    INSERT [tb]
    SELECT 1,N'2006-06-11 05:52:12',1360065460758 UNION ALL
    --新增记录
    SELECT 12,N'2006-06-11 05:53:12',136006546058 UNION ALL
    SELECT 13,N'2006-06-11 05:54:12',136006546758 UNION ALL
    SELECT 14,N'2006-06-11 05:55:12',136006540758 UNION ALL
    SELECT 15,N'2006-06-11 05:54:12',136006560758 UNION ALL
    -------
    SELECT 16,N'2006-06-11 05:57:11',1360065460758 UNION ALL--精确到分归入后面的记录,精确到秒归到前面的记录
    -------
    SELECT 2,N'2006-06-11 06:09:05',1380138017292 UNION ALL
    SELECT 3,N'2006-06-11 06:25:13',97363849310 UNION ALL
    SELECT 4,N'2006-06-11 06:26:08',61763849310 UNION ALL
    SELECT 5,N'2006-06-11 06:34:59',60054190811 UNION ALL
    SELECT 6,N'2006-06-11 06:44:04',1397305138553 UNION ALL
    SELECT 7,N'2006-06-11 06:51:30',60063849310 UNION ALL
    SELECT 8,N'2006-06-11 06:53:14',92713061620671 UNION ALL
    SELECT 9,N'2006-06-11 06:53:22',11713061620671 UNION ALL
    SELECT 10,N'2006-06-11 06:58:34',32054196057 UNION ALL
    SELECT 11,N'2006-06-11 07:04:42',92752581156
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    --此查询精确到分:
    SELECT '第'+LTRIM(N+1)+'个5分钟' AS [5分钟系列],
        COUNT(1) AS 记录数
    FROM (
        SELECT 
            N=DATEDIFF(N,(SELECT MIN(TIME) FROM TB),TIME)/5 
        FROM TB
        ) AS T
    GROUP BY N
    /*
    5分钟系列                 记录数
    --------------------- -----------
    第1个5分钟                5
    第2个5分钟                1
    第4个5分钟                1
    第7个5分钟                2
    第9个5分钟                1
    第11个5分钟               1
    第12个5分钟               1
    第13个5分钟               2
    第14个5分钟               1
    第15个5分钟               1(10 行受影响)
    */
    --此查询精确到秒:
    SELECT '第'+LTRIM(N+1)+'个5分钟' AS [5分钟系列],
        COUNT(1) AS 记录数
    FROM (
        SELECT 
            N=DATEDIFF(SS,(SELECT MIN(TIME) FROM TB),TIME)/300 
        FROM TB
        ) AS T
    GROUP BY N
    /*
    5分钟系列                 记录数
    --------------------- -----------
    第1个5分钟                6
    第4个5分钟                1
    第7个5分钟                2
    第9个5分钟                1
    第11个5分钟               1
    第12个5分钟               1
    第13个5分钟               2
    第14个5分钟               1
    第15个5分钟               1(9 行受影响)
    */