在 oracle sql 中使用排名函数显示空值
display null value using rank functions in oracle sql
这是另一个问题的扩展:
我有一个 table 设置如下:
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
有两个tax 1和2。所以Element1
应该显示一次,c_tax
应该是yes。
Element3
没有税,因此应显示为 null
。
我的输出应该如下所示:
c_id c_name c_tax
1001 Element1 Yes
null Element2 Yes
1002 Element3 No
null Element4 Yes
如果我理解正确的话:
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;
这是另一个问题的扩展:
我有一个 table 设置如下:
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
有两个tax 1和2。所以Element1
应该显示一次,c_tax
应该是yes。
Element3
没有税,因此应显示为 null
。
我的输出应该如下所示:
c_id c_name c_tax
1001 Element1 Yes
null Element2 Yes
1002 Element3 No
null Element4 Yes
如果我理解正确的话:
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;