首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

用sql查询如下要求的数据怎么写代码

2012-03-28 
用sql查询如下要求的数据如何写代码有如下数据,C列为时间单位为秒,现想找出相差三秒中内A中同个数据出现三

用sql查询如下要求的数据如何写代码
有如下数据,C列为时间单位为秒,现想找出相差三秒中内A中同个数据出现三次的记录数。可能A列中相同的数据很多,但是只要C列中数值相差三秒钟内包含三秒的情况下,A列相同数据出现三次,将这三次的数据找出来即可,之前我也发帖过,但是问题弄错,谢谢大家
ABC(秒)
12322223
22322123
22325626
1232227
12312629
223225558
22325625
12322330
通过查找结果为:
ABC(秒)
22322123
22325626
1232227
12312629
22325625
12322330
请问sql查询如何书写,谢谢大家


[解决办法]

SQL code
create table tb(A int,B int,C int)insert into tb select 123,222,23insert into tb select 223,221,23insert into tb select 223,256,26insert into tb select 123,22,27insert into tb select 123,126,29insert into tb select 223,225,558insert into tb select 223,256,25insert into tb select 123,223,30goselect a.* from tb a inner join(select * from tb a where exists(select 1 from tb where a=a.a and c<>a.c and c-a.c between 1 and 3)and exists(select 1 from tb where a=a.a and c<>a.c and a.c-c between 1 and 3))b on a.a=b.a and abs(a.c-b.c)<=3/*A           B           C----------- ----------- -----------123         22          27123         126         29123         223         30223         221         23223         256         26223         256         25(6 行受影响)*/godrop table tb
[解决办法]
没赶上
[解决办法]
SQL code
create table tb(A int,B int,C int)insert into tb select 123,222,23insert into tb select 223,221,23insert into tb select 223,256,26insert into tb select 123,22,27insert into tb select 123,126,29insert into tb select 223,225,558insert into tb select 223,256,25insert into tb select 123,223,30goselect a.* from tb a inner join(select a,c from tb twhere exists(select 1 from tb where a=t.a and c<>t.c and c-t.c between 0 and 3 group by a having count(*)=2))b on a.a=b.a and a.c-b.c between 0 and 3/*A           B           C----------- ----------- -----------123         22          27123         126         29123         223         30223         221         23223         256         26223         256         25(6 行受影响)*/godrop table tb 

热点排行