日期:2014-05-17  浏览次数:20528 次

查询时间间隔大于5分钟的数据
SQL code
USE tempdb 
IF OBJECT_ID('TEST') IS NOT NULL
  DROP TABLE TEST;      --> 如果表TEST不为空,删除表
GO  
CREATE TABLE TEST       --> 创建表,字段及类型如下
(
  dtime  datetime,
  name   char(3)
);
GO
INSERT INTO TEST VALUES --> 向表中插入数据
('2012-9-9 12:00','aaa'),
('2012-9-9 12:03','aaa'),
('2012-9-9 12:06','aaa'),
('2012-9-9 12:09','aaa'),
('2012-9-9 12:12','aaa'),
('2012-9-9 12:00','bbb'),
('2012-9-9 12:04','bbb'),
('2012-9-9 12:10','bbb');
GO 
SELECT * FROM TEST;      --> 表的结构如下
GO
/*
dtime                   name
----------------------- ----
2012-09-09 12:00:00.000 aaa
2012-09-09 12:03:00.000 aaa
2012-09-09 12:06:00.000 aaa
2012-09-09 12:09:00.000 aaa
2012-09-09 12:12:00.000 aaa
2012-09-09 12:00:00.000 bbb
2012-09-09 12:04:00.000 bbb
2012-09-09 12:10:00.000 bbb
*/
--------------------开始查询--------------------


 
--------------------期待结果--------------------
/*
dtime                   name
----------------------- ----
2012-09-09 12:00:00.000 aaa
2012-09-09 12:06:00.000 aaa
2012-09-09 12:12:00.000 aaa
2012-09-09 12:00:00.000 bbb
2012-09-09 12:10:00.000 bbb
*/


------解决方案--------------------
暂时只想到用循环 一行一行判断咯

------解决方案--------------------
本来不想用循环的,奈何水平有限,再给我几天思考一下,暂时贴上个循环的方案
SQL code
select distinct a.name,b.dtime 
into #ta
from test a 
cross apply (select top(1) dtime from test where name = a.name order by dtime )b

while 1 = 1
begin
insert into #ta
select b.name,b.dtime from
(select name,max(dtime)dtime from #ta group by name)a cross apply 
(select top(1) name, dtime from test where name = a.name and datediff(mi,a.dtime,dtime)> 5 order by dtime )b
 IF @@rowcount < 1 BREAK
end
select * from #ta order by name,dtime
/*name dtime
---- -----------------------
aaa  2012-09-09 12:00:00.000
aaa  2012-09-09 12:06:00.000
aaa  2012-09-09 12:12:00.000
bbb  2012-09-09 12:00:00.000
bbb  2012-09-09 12:10:00.000

(5 行受影响)*/

------解决方案--------------------
测了一下37楼的,数据有误
SQL code
select distinct name,dtime
from
(
select *,datepart(minute,dtime-(select min(dtime) from test where t.name=name))minute from test t
)A
where minute=0 or minute>5
order by name,dtime
/*name dtime
---- -----------------------
aaa  2010-06-01 00:05:00.000
aaa  2010-06-01 00:28:00.000
aaa  2010-06-01 00:34:00.000---
aaa  2010-06-01 00:38:00.000---这里不符合题意
aaa  2010-06-01 00:42:00.000---
aaa  2010-06-01 00:53:00.000
aaa  2010-06-01 00:57:00.000
aaa  2010-06-01 00:59:00.000
aaa  2010-06-01 01:04:00.000
aaa  2010-06-01 01:05:00.000
aaa  2010-06-01 01:15:00.000
aaa  2010-06-01 01:16:00.000
aaa  2010-06-01 01:17:00.000
aaa  2010-06-01 01:25:00.000
aaa  2010-06-01 01:31:00.000*/


--用于生成示例数据
if OBJECT_ID('Test') is not null drop table Test
create table Test(id int identity(1,1), dtime  datetime,
  name   char(3))
INSERT INTO dbo.Test SELECT  dateadd(MI,1+abs(checksum(newid()))%(900), '2010-06-01'), 
        
        case when abs(checksum(newid()))%(5) = 0 then 'aaa' 
        when abs(checksum(newid()))%(4) = 0 then 'bbb'
        when abs(checksum(newid()))%(3) = 0 then 'ccc'
        when abs(checksum(newid()))%(2) = 0 then 'ddd' 
        else 'eee' end

DECLARE @i AS INT, @rc AS INT,@max int;
SET @rc = 1;
SET @max = 1000;
set @i = ceiling(log10(@max)/LOG10(2))-1;
WHILE @rc  <= @i
BEGIN
    INSERT INTO dbo.Test SELECT  dateadd(MI,1+abs(checksum(newid()))%(900), '2010-06-01'), 
        
        case when abs(checksum(newid()))%(5) = 0 then 'aaa' 
        when abs(checksum(newid()))%(4) = 0 then 'bbb'
        when abs(checksum(newid()))%(3) = 0 then 'ccc'
        when abs(checksum(newid()))%(2) = 0 then 'ddd' 
        else 'eee' end
  FROM dbo.Test;
  SET @rc = @rc +1 ;
END
select @max = @max - @@IDENTITY
INSERT INTO dbo.Test 
  SELECT top(@max) dateadd(MI,1+ab