2013-11-21 54 views
0

如何從表/子查詢中選擇左上角不空值?ORACLE表左上不爲空值

EXPL:

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

我只需要值 '1'

回答

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

如果你不那麼請詳細解釋你的要求是什麼。

0

我的回答假設你要訂購一個山坳值的結果,否則,有沒有知道什麼是結果的順序方式:

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