我有以下表,最大日期,并在同一个表中的最大日之前的日期选择记录
R_ID DATE Col_A Col_B Col_C
158 20161008 01 01 99
158 20161012 01 01 99
158 20161019 01 02 10
158 20161022 99 01 10
我想选择这样,我得到以下结果
R_ID DATE Col_A Col_B Col_C
158 20161008 01 01 99
158 20161022 99 01 10
的这里的逻辑是
1. 'select max date' for record with Col_C = '10' for a particular R_ID and
2. When Col_A or Col_B = '01' and Col_C <> '10' select the minimum Date which is < Max_date used in 1st condition
我使用愈合状况像下面
Select * from tbl1 T
where
T.Col_C = '10' and
T.DATE = (select max(T2.DATE) from tbl1 T2
where
T2.Col_C = '10' and
T3.R_ID = T.R_ID
)
union
Select * from tbl1 K
where
(K.Col_A = '01' or K.Col_B = '01') and
K.Col_C <> '10' and
K.DATE = (select min(K2.DATE) from tbl1 K2 where
(K2.Col_A = '01' or K2.Col_B = '01') and
K2.Col_C <> '10' and
K2.R_ID = K.R_ID
) and
--K.DATE < T.DATE-- How do I use this condition within union?
我希望能够到注释中使用的条件,但我找不出正确的语法
我已经更新了状态
您的规则和您的示例结果不匹配。 –