2017-10-19 103 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