CASE语句,其中一行中的数据取决于另一行

时间:2016-07-28 21:03:22

标签: sql oracle

我在视图中显示以下数据:

enter image description here

我正在寻找一些代码,如果FEB的PM_NO 4为年度,那么每月,季度和半年都是空的。如果MAY的PM_NO 2是QUARTERLY然后MONTHLY为null&out; d out等,那么同样适用。这可能在Oracle SQL上。

此致

Jon Ditchfield

1 个答案:

答案 0 :(得分:0)

一种方法是为每个具有值的列找到最后一个pm_no,然后将所有早期的列设置为NULL:

select t.pm_no,
       (case when pm_no >= jan_pmno then jan end) as jan,
       (case when pm_no >= feb_pmno then feb end) as feb,
       . . .
from (select t.*,
             max(case when jan is not null then pm_no end) over () as jan_pmno,
             max(case when feb is not null then pm_no end) over () as feb_pmno,
             . . .
      from t
     ) t
相关问题