with type_a as
(select distinct id, name, type
from table_name
where type = 'A'
),
type_b as
(select distinct id, name, type
from table_name
where type = 'B'
and id not in (select id from type_a)
)
select * from type_a
union
select * from type_b
select t.*
from tablename t
where t.type = 'A'
or not exists (select 1 from tablename where id = t.id and name = t.name and type = 'A')
如果 name 不应涉及该情况,则使用以下方法:
or not exists (select 1 from tablename where id = t.id and type = 'A')
或使用 RANK() 窗口功能:
select t.id, t.name, t.type
from (
select t.*
rank() over (partition by id, name order by case when type = 'A' then 1 else 2 end) rnk
from tablename
) t
where t.rnk = 1
2条答案
按热度按时间uubf1zoe1#
有一种方法。。。
3b6akqbq2#
使用
NOT EXISTS
:如果
name
不应涉及该情况,则使用以下方法:或使用
RANK()
窗口功能:或删除
name
从partition
如果不相关。