2017-10-19 125 views
1

這是另一個問題的擴展:How to display null value when the record is present more than one time in oracle sql顯示空值

我有一個表中設置像以下:

c_id c_name  c_tax 

1001 Element1 1 
1001 Element1 2 
1001 Element2 1 
1001 Element2 2 
1002 Element3 null 
1002 Element4 1 
1002 Element4 2 

我想在第一column(c_id)如果顯示null根據以下條件在第三個column(c_tax)中出現多次,是或否。

Element1有兩種稅收1和2.所以Element1應該顯示一次,c_tax應該是。 Element3不含稅,所以應該顯示爲null

我的輸出結果應類似於以下內容:

c_id c_name  c_tax 

1001 Element1 Yes 
null Element2 Yes 
1002 Element3 No 
null Element4 Yes 
+0

內部stackoverlow鏈接應粘貼生,而不是使用鏈接的格式。 –

回答

1

如果我理解正確:

select (case when row_number() over (partition by cid order by c_name) = 1 then cid end) as cid, 
     c_name, 
     (case when max(c_tax) is not null then 'yes' else 'no' end) as c_tax 
from t 
group by c_id, c_name 
order by c_id, c_name; 
+0

不僅你理解正確,你的答案是完美的。感謝很多@Gordon Linoff – Santosh