【发布时间】:2017-04-18 16:08:54
【问题描述】:
我有下表
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?
我希望能够在评论中使用条件,但无法确定正确的语法
我已经更新了条件
【问题讨论】:
-
您的规则和您的样本结果不匹配。
标签: sql oracle select join union