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

sql查找在一定时间范围内相同数据出现一定次数的数据,该怎么处理

2012-04-06 
sql查找在一定时间范围内相同数据出现一定次数的数据之前类似的情况我发帖问过了,但是我现在有如下数据,C

sql查找在一定时间范围内相同数据出现一定次数的数据
之前类似的情况我发帖问过了,但是我现在有如下数据,C列为时间单位为秒,想找出三秒内A中同个数据出现三次的记录数,不用管B列数据,将这三次的数据找出来即可,谢谢大家。
A B C(秒)
123 222 23
223 221 23
223 256 26
123 22 27
123 126 29
223 225 558
223 256 25
123 223 30
通过查找结果为:
A B C(秒)
223 221 23
223 256 26
123 22 27
123 126 29
223 256 25
123 223 30
请问sql查询如何书写,谢谢大家


[解决办法]
select * from tb1
where a in (select a from tb1 group by a having count(*)>=3)
[解决办法]
select * from tb1
where a in (select a from tb1 group by a having count(*)>=3) and c<3
[解决办法]

SQL code
------------------------------ Author  :fredrickhu(小F,向高手学习)-- Date    :2011-12-07 08:48:14-- Version:--      Microsoft SQL Server 2008 R2 (RTM) - 10.50.1617.0 (Intel X86) --    Apr 22 2011 11:57:00 --    Copyright (c) Microsoft Corporation--    Enterprise Evaluation Edition on Windows NT 6.1 <X64> (Build 7600: ) (WOW64)--------------------------------> 测试数据:[tb]if object_id('[tb]') is not null drop table [tb]go create table [tb]([A] int,[B] int,[C] int)insert [tb]select 123,222,23 union allselect 223,221,23 union allselect 223,256,26 union allselect 123,22,27 union allselect 123,126,29 union allselect 223,225,558 union allselect 223,256,25 union allselect 123,223,30--------------开始查询--------------------------select   distinct a.*from   tb a,tb bwhere   a.A in(select a from tb group by  a having COUNT(1)>=3)and   exists(select 1 from tb where a=b.A and a.C<=b.C-3)and   a.A=b.A----------------结果----------------------------/* A           B           C----------- ----------- -----------123         22          27123         222         23223         221         23223         256         25223         256         26(5 行受影响)*/
[解决办法]
上次的回答:

select 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

不满足需求吗?


[解决办法]
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 

热点排行
Bad Request.