如何解决案例查询中基于计数的返回值
我很期待你对这个请求的意见,看起来超级简单,但我一个人无法弄清楚。
如果表列part_status只有一个值,则返回;否则返回“许多”。
场景 1 表数据:
Part_Status
'A'
'A'
'A'
退货
Part_Status
'A'
场景 2 表数据:
Part_Status
'A'
在此处返回 col1 值
Part_Status
'A'
场景 3 表数据:
Part_Status
'A'
'E'
返回值
Part_Status
'Many'
第一个想法是使用 case,如果计数的数量大于 1,则返回 'many' 但它从不打印:
select DISTINCT case
when count(PART_STATUS) > 1 THEN
'MANY'
ELSE
PART_STATUS
end
from (select DISTINCT Part_Status
from Inventory_Part
where part_status IN ('A','E')
)
GROUP BY PART_STATUS
退货
Part_Status
'A'
'E'
在第二个想法中,我得到了“许多”工作,但单个结果为 NULL
select CASE
WHEN COUNT(PART_STATUS) > 1 THEN
'Many'
end AS "PART_STATUS"
FROM (select DISTINCT Part_Status
from Inventory_Part
where part_status IN ('A','E'))
退货
Part_Status
'Many'
那么,如何将两者结合起来?
解决方法
类似的东西?
SQL> with test (part_status) as
2 (select 'A' from dual union all
3 select 'A' from dual
4 )
5 select distinct
6 case when (select count(distinct part_status) from test) > 1 then 'Many'
7 else part_status
8 end
9 from test;
CASE
----
A
SQL> with test (part_status) as
2 (select 'A' from dual union all
3 select 'E' from dual
4 )
5 select distinct
6 case when (select count(distinct part_status) from test) > 1 then 'Many'
7 else part_status
8 end
9 from test;
CASE
----
Many
SQL>
,
我可以解决它。无论如何,我不相信这是聪明的方法。
select CASE
when "cnt" > 1 THEN
'Many'
when "cnt" = 1 THEN
PART_STATUS
end
from (select rownum AS "cnt",PART_STATUS
FROM (select DISTINCT Part_Status
from Inventory_Part
where part_status IN ('A','E'))
ORDER BY ROWNUM DESC)
fetch first row only
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。