【发布时间】:2021-12-08 15:58:48
【问题描述】:
我需要根据数据类型对一些数据进行分组并考虑优先级。
以下面的 CTE 为例。
WITH classif AS
(
select 1 as id, 'account' as type, 'high' as priority from dual union all
select 2 as id, 'account' as type, 'none' as priority from dual union all
select 3 as id, 'account' as type, 'medium' as priority from dual union all
select 4 as id, 'security' as type, 'high' as priority from dual union all
select 5 as id, 'security' as type, 'medium' as priority from dual union all
select 6 as id, 'security' as type, 'low' as priority from dual union all
select 7 as id, 'security' as type, 'none' as priority from dual union all
select 8 as id, 'transform' as type, 'none' as priority from dual union all
select 9 as id, 'transform' as type, 'none' as priority from dual union all
select 10 as id, 'transform' as type, 'none' as priority from dual union all
select 11 as id, 'transform' as type, 'none' as priority from dual union all
select 12 as id, 'enrollment' as type, 'medium' as priority from dual union all
select 13 as id, 'enrollment' as type, 'low' as priority from dual union all
select 14 as id, 'enrollment' as type, 'low' as priority from dual union all
select 15 as id, 'enrollment' as type, 'low' as priority from dual;
select 15 as id, 'process' as type, 'low' as priority from dual;
select 15 as id, 'process' as type, 'none' as priority from dual;
select 15 as id, 'process' as type, 'none' as priority from dual;
)
对于这个数据集,我的输出必须是这样的
------------+-------------
type | priority
------------+-------------
account | high
security | high
transform | none
enrollment | medium
process | low
---------------------------
优先级从“高”到“无”
输出的规则一定是这样的
- 当一个类型有一个优先级为“high”的行时,该类型的输出必须是“high”。
- 当一个类型有一个优先级为“中”的行并且没有其他的 使用“高”,输出必须为“中”。
- 当一个类型有一个优先级“低”的行并且没有其他的 使用“高”或“中”,输出必须为“低”。
- 当一个类型有一个优先级为“none”的行并且没有任何 其他,输出必须为“无”
我正在尝试执行类似下面的查询,但这将返回所有行而不是根据优先级进行分组
select
type,
case
when priority = 'high' then 'high'
when priority = 'medium' and priority <> 'high' then 'medium'
when priority = 'medium' and priority <> 'high' then 'medium'
when priority = 'low' and priority <> 'high' or priority <> 'medium' then 'low'
when priority = 'none' and priority <> 'high' or priority <> 'medium' or priority <> 'low' then 'none'
end as priority
from classif
group by type,
case
when priority = 'high' then 'high'
when priority = 'medium' and priority <> 'high' then 'medium'
when priority = 'medium' and priority <> 'high' then 'medium'
when priority = 'low' and priority <> 'high' or priority <> 'medium' then 'low'
when priority = 'none' and priority <> 'high' or priority <> 'medium' or priority <> 'low' then 'none'
end;
你能帮我在查询中解决这个问题吗?
【问题讨论】:
-
您最好创建一个映射表,而不是尝试处理查询中的条件值。
标签: sql oracle greatest-n-per-group