2017-05-23 80 views
-5

我已經寫在甲骨文的SQL查詢返回的結果如下:的Oracle SQL結果

accountName1 null 
null   hostName 
accountName2 null 
null   hostName 

相反,我想結果看起來像:

accountName1 hostName 
accountName2 hostName 
+2

沒有你當前的查詢,表結構和原始數據,y怎麼做你希望我們能夠幫助你嗎?實際上總是顯示相同的「hostname」值,還是它們不同? –

回答

1

假設一個或其它列值始終爲NULL then:

SELECT MAX(account_name) AS account_name, 
     MAX(host_name ) AS host_name 
FROM (
    SELECT t.*, 
     NVL2(
      account_name, 
      ROW_NUMBER() OVER (ORDER BY NVL2(account_name, ROWNUM, NULL) NULLS LAST), 
      ROW_NUMBER() OVER (ORDER BY NVL2(host_name, ROWNUM, NULL) NULLS LAST) 
     ) AS rn 
    FROM your_table t 
) 
GROUP BY rn;