根据第一个选择查询结果从两个不同的选择查询结果(Oracle)中选择一个值

时间:2019-11-29 03:41:04

标签: sql oracle

请根据我的要求修改以下查询。请在下面找到代码。

CASE Select NVL(Sum(Value1+value2 +value3),0) from Table1 Where Step = 4
WHEN 0 THEN (Select Sum(Value1) from Table1 Where Step < 4)
Else NVL(Sum(Value1 + value2 + value3) END AS "SumValue"

1 个答案:

答案 0 :(得分:1)

这是你的去。使用subquery

select case when t1.val = 0 then t1.val2 else t1.val3 end as "SumValue"
from
    (Select NVL(Sum(case when step = 4 then Value1+value2+value3 else 0 end),0) val
        , Sum(case when step < 4 then Value1 else 0 end) val2
        , Sum(Value1+value2+value3) val3 from Table1) t1