在下一列值中显示第一列值,直到遇到非空值

时间:2013-02-15 11:33:52

标签: oracle11g

我的表part_volume包含column part,volume_1,volume_2,volume_3,volume_4,volume_5,volume_6列。

表格包含以下数据:

PART      VOLUME_1 VOLUME_2 VOLUME_3 VOLUME_4 VOLUME_5 VOLUME_6               
--------- -------- -------- -------- -------- -------- --------
camera        1500        0        0        0     3000        0

我想将数据显示为

PART      VOLUME_1 VOLUME_2 VOLUME_3 VOLUME_4 VOLUME_5 VOLUME_6               
--------- -------- -------- -------- -------- -------- --------
camera        1500     1500     1500     1500     3000     3000    

我怎样才能在oracle中实现这一目标?

谢谢和问候,

洛帕

1 个答案:

答案 0 :(得分:0)

所以如果当前值为0,请保留先前值?

如果是这样的话:

select part,
       volume_1,
       coalesce(volume_2, volume_1) volume_2,
       coalesce(volume_3, volume_2, volume_1) volume_3,
       coalesce(volume_4, volume_3, volume_2, volume_1) volume_4,
       coalesce(volume_5, volume_4, volume_3, volume_2, volume_1) volume_5,
       coalesce(volume_6, volume_5, volume_4, volume_3, volume_2, volume_1) volume_6
  from (select part,
               volume_1,
               case volume_2 when 0 then to_number(null) else volume_2 end volume_2,
               case volume_3 when 0 then to_number(null) else volume_3 end volume_3,
               case volume_4 when 0 then to_number(null) else volume_4 end volume_4,
               case volume_5 when 0 then to_number(null) else volume_5 end volume_5,
               case volume_6 when 0 then to_number(null) else volume_6 end volume_6
          from data);

虽然可以为空,如果是,那么规则是否为零?