數據庫是: MS SQL Severl
有一這樣的表 a
表a 的 field 和 values 如下:
時間,地點,發生的事件 為關鍵字
時間 地點 發生的事件
2003-09-01 aa 1
2003-09-01 bb 2
2003-09-02 aa 3
2003-09-03 cc 1
2003-09-03 cc 2
2003-09-03 cc 3
我想要的結果是這樣的:
時間 地點 發生的事件
2003-09-03 cc 1
2003-09-03 cc 2
2003-09-03 cc 3
哪位朋友可幫我一下,用一條SQL語句搞定
如果是sql server数据库或者access数据库,可以用下面的语句:
select a.* From a
left join (select 時間,地點,count(發生的事件) from a group by 時間,地點 having count(發生的事件)>1) t
on a.時間=b.時間 and a.地點=b.地點
Select *
From a
Where 时间 in ( select 时间
from ( select 时间,count(*) as b
from a
group by 时间) c
where b>1 ) And
地点 in ( select 地点
from ( select 地点,count(*) as b1
from a
group by 地点) d
where b1>1 )
select a.时间,a.地点, a.發生的事件,c.时间,c.地点, c.發生的事件 from a b,a c where a.地点=c.地点 and a.时间=c.时间 and exists (select * from a,d group by having count(*)>=2)
select a.时间,a.地点, a.發生的事件 from a where exists (select * from a group by 时间,地点 having count(*)>=2)
select a.时间,a.地点, a.發生的事件 from a where exists (select * from a group by 时间,地点 having count(*)>=2) order by a.时间,a.地点