如何使用条件从sql表中删除重复项

ej83mcc0  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(535)

我有下面的sql表,我想删除重复的条目时 object_id 比赛 team_name . 基本上我想要的是 object_id ```
session_id object_id team_name user_name user_desc


session1 user1 team1 user1 user1_description
session1 user2 team1 user2 user2_description
session1 team1 team1 user1 user1_description
session1 team1 team1 user2 user2_description

我想把上表转换成下表

session_id object_id team_name user_name user_desc


session1 user1 team1 user1 user1_description
session1 user2 team1 user2 user2_description
session1 team1 team1 null null

我要怎么做?
vc6uscn9

vc6uscn91#

如果我理解正确,您可以使用聚合:

select (case when min(session_id) = max(session_id) then min(session_id) end) as session_id,
       object_id,
       (case when min(team_name) = max(team_name) then min(team_name) end) as team_name,
       (case when min(user_name) = max(user_name) then min(user_name) end) as user_name,
       (case when min(user_desc) = max(user_desc) then min(user_desc) end) as user_desc
from t
group by object_id;

相关问题