我有一张大约有800万行的表格。行包含ID,日期和事件代码。我想选择事件代码等于1的所有ID和日期,并且在过去的某个时间有一个等于2的事件代码。
例如,我的表格如下:
ID Date Code
----------------------
1 4/16/2016 6
1 4/10/2016 1
1 3/1/2016 13
1 1/26/2016 2
2 5/2/2016 8
2 3/14/2016 1
2 1/13/2016 14
我希望ID = 1并返回Date = 4/10/2016,但我不希望返回ID = 2的任何内容,因为ID = 2从未有过等于2的事件代码。
我应该如何编写SELECT
语句来获得这些结果?
答案 0 :(得分:1)
您可以使用exists
。
select *
from t
where code = 1 and
exists (select 1 from t t1 where t.id = t1.id and t.dt > t1.dt and t1.code=2)
答案 1 :(得分:1)
如果您只想为每个ID
选择最长日期:
WITH Cte AS(
SELECT *,
rn = ROW_NUMBER() OVER(PARTITION BY ID ORDER BY Date DESC)
FROM tbl
WHERE Code = 1
)
SELECT
ID, Date, Code
FROM Cte c
WHERE
rn = 1
AND EXISTS(
SELECT 1
FROM tbl t
WHERE
t.ID = c.ID
AND t.Date < c.Date
AND t.Code = 2
)
;
使用MAX
,GROUP BY
和HAVING
:
SELECT
ID, Date = MAX(Date)
FROM tbl t1
WHERE Code = 1
GROUP BY t1.ID
HAVING MAX(Date) > (SELECT Date FROM tbl t2 WHERE t2.ID = t1.ID AND t2.Code = 2)
答案 2 :(得分:0)
select id
, max(date)
from table to
where to.code = 1
and exists (select 1 from table ti where ti.id = to.id AND ti.code = 2)
group by id
答案 3 :(得分:0)
@vkp有最好的答案。但是,这是一种使用窗口函数的方法:
select t.id, t.code, t.dt
from (select t.*, min(case when code = 2 then dt end) over (partition by id) as dt_2
from t
where code in (1, 2)
) t
where t.code = 1 and dt_2 < dt;