我有一张这样的桌子,
ID Quarter Risk
00001 Q0 2
00001 Q1 3
00001 Q2 1
00001 Q3 1
00001 Q4 2
1 means Lower Risk and 3 means Higher Risk。以及Q0 means 4th Quarter of previous year
我想要这样的输出:
ID Quarter Risk Status 00001
Q0 2 null 00001 Q1
3 Upgraded 00001 Q2 1
Downgraded00001 Q3 1 No Change
00001 Q4 2 Upgraded

如果风险高于上一季度,那么它将被升级或降级。如果值保持不变,则无变化。
我只想在oracle 10g中使用sql来实现这一点。
损益表/SQL。
有人能帮我一下吗?

最佳答案

您可以使用lag()

select t.*,
       (case when Risk > prev_rsk
             then 'Upgraded'
             when Risk < prev_rsk
             then 'Downgraded'
             when Risk = prev_rsk
             then 'No Change'
        end) as Status
from (select t.*,
             lag(Risk) over (partition by id order by Quarter) as prev_rsk
      from table t
     ) t;

关于sql - 仅使用SQl比较Oracle中按行的值更改,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53540973/

10-12 19:42