以下是我提到的数据库:
ID TFD1 Date CCA
D-1 123 25-01-2017 AA
D-2 123 24-01-2017 BA
D-3 123 27-01-2017 BB
D-4 456 15-01-2017 AA
D-5 456 13-01-2017 BS
D-6 789 11-01-2017 AA我想获取那些ID、TFD1、Date和CCA,其中至少有一个与TFD1对应的值是AA,而与TFD1对应的所有其他旧值都不是AA。
我想要创建按最老到最新排序的输出。
所需产出:
ID TFD1 Date CCA
D-6 789 11-01-2017 AA
D-1 123 24-01-2017 BA
D-2 123 25-01-2017 AA
D-4 456 13-01-2017 BS
D-5 456 15-01-2017 AA发布于 2018-03-16 14:39:15
为了得到你可以使用的“A”字:
select t.*
from t
where (t.cca = 'A' and
not exists (select 1 from t t2 where t2.tfd1 = t.tfd1 and t.cca = 'A')
) ;由此,您可以在此日期之前获得每个tfd1的所有内容。
select t.*
from t join
(select t.*
from t
where (t.cca = 'A' and
not exists (select 1
from t t2
where t2.tfd1 = t.tfd1 and t.cca = 'A' and t2.date < t1.date
)
)
) tt
on t.tfd1 = tt.tfd1 and t.date <= tt.date;https://stackoverflow.com/questions/49322067
复制相似问题