0

我有一些条件“更简单”的子选择,但这超出了我的范围。所以我在加入后有一张桌子......

A  B    C
1  x  2013.01
1  y  2013.02
1  z  2013.03
2  x  2013.02
2  x  2013.05
2  y  2013.06
2  z  2013.08
2  z  2013.09
3  y  2013.02
3  z  2013.03
3  x  2013.04

所以我需要那些存在 z 的 id-s,但之前不存在 x。

所以结果只有 3 因为 z 存在而 x 之前不存在。

4

1 回答 1

1
select *
from Table1 t1 
     left join Table1 t2 ON t1.A = t2.A and t2.B = 'x'
where t1.B = 'z'
    and ( t2.C >= t1.C or t2.A is null );

看到它在sqlfiddle中实时工作

于 2014-08-13T14:16:53.733 回答