我有以下表,最大日期,並在同一個表中的最大日之前的日期選擇記錄
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?
我希望能夠到註釋中使用的條件,但我找不出正確的語法
我已經更新了狀態
您的規則和您的示例結果不匹配。 –