ORACLE表左上方没有空值

时间:2013-11-21 22:43:05

标签: sql oracle

如何从表/子查询中选择左上角非空值?

探索:

colfkey     col1 | col2 | col3 | col4  
xxxxxxx     null   null   '1'    null  
xxxxxxx     null    '2'   null   null   

我只需要'1'值

2 个答案:

答案 0 :(得分:0)

你的意思是这样的:

SELECT colfkey, COALESCE( col1, col2, col3, col4 ) AS value
FROM   table_name
WHERE  ROWNUM = 1
AND    COALESCE( col1, col2, col3, col4 ) IS NOT NULL;

SQL FIDDLE

如果您不这样做,请详细说明您的要求。

答案 1 :(得分:0)

我的回答是假设您想要将结果排序一个col值,否则,无法知道结果的顺序是什么:

SELECT * FROM
 (SELECT CASE WHEN "col1" IS NOT NULL THEN "col1"
   WHEN "col2" IS NOT NULL THEN "col2"
   WHEN "col3" IS NOT NULL THEN "col3"
   WHEN "col4" IS NOT NULL THEN "col4" END AS TOP_VALUE
   FROM expl
 ORDER BY 1 ASC) a
WHERE rownum = 1

sqlfiddle demo