sql查询在表中查找第二个非空值(oracle)

时间:2012-12-04 18:02:25

标签: sql oracle

我有一个有n列的n列的表。列名的一些如下

c, c2,c3,c4,c5  , c25

sample data
c1    c2     c3   c4  c5  (consider only 5 in this case)
x     y      z    z  y
x     y
x
a     b     j     k
a     c      g    h  i
k     l      m    n  o

现在op =第二个不是右边的值 对上述数据进行采样

z
x
x  (special case as no data left of x)
j
h
n 

不能使用COALESCE广告我需要第二个不是空而不是第一个

有人可以帮我解决这个问题吗

2 个答案:

答案 0 :(得分:3)

您可以使用更复杂的case语句执行此操作:

select (case when c5 is not null
             then coalesce(c4, c3, c2, c1)
             when c4 is not null
             then coalesce(c3, c2, c1)
             when c3 is not null
             then coalesce(c2, c1)
             else c1
        end)
. . .

答案 1 :(得分:1)

类似的东西:

 select nvl2(c5,c4,nvl2(c4,c3,nvl2(c3,c2,c1))) result
 from   my_table

未经测试。