表结构如下:
++ID++++READ_ID++++READ_TYPE
101 201 30
102 201 35
103 201 40
104 201 60
105 202 50
106 202 60
我需要根据以下条件选择READ_TYPE:
条件1:检查每个READ_ID是否存在30,35或40。如果存在,请在30、35和40之间选择最大的READ_TYPE。例如READ_ID 201具有30、35、40和60。结果必须为40。
条件2:如果不存在30、35或40,则获取read_type的最大值。例如,READ_ID 202具有50和60。结果必须为60。
如何从单个oracle SQL查询中实现。
您可以使用条件聚合来做到这一点:
select read_id,
(case when sum(case when read_type in (30, 35, 40) then 1 else 0 end) > 0
then max(case when read_type in (30, 35, 40) then read_type end)
else max(read_type)
end) as themax
from t
group by read_id;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句