【问题标题】:Eliminating similar row but with specific values消除类似行但具有特定值
【发布时间】:2012-12-03 04:56:30
【问题描述】:

我有 user 表和 user_group 表。单个用户可以在多个组中。我有两个组管理员和用户。管理员组的组 ID 为 1,用户组的组 ID 为 2。当我选择加入这两个表时。我想使用两个表之间的连接来选择它:

select * from users u
join user_groups ug
on u.user_id = ug.user_id;

我在每行中两次获得管理员。我想以组 id 作为用户即 1 在此查询中为管理员消除其他行,我该如何实现? sqlfiddlehere。组相关信息在 all_groups 表中。

【问题讨论】:

    标签: sql oracle11g analytic-functions


    【解决方案1】:

    假设要保留group_id 值最大的行

    select *
      from (select u.user_id,
                   u.username,
                   u.first_name,
                   u.middle_name,
                   u.last_name,
                   u.password,
                   ug.group_id,
                   rank() over (partition by ug.user_id
                                    order by ug.group_id desc) rnk
              from users u
                   join user_groups ug
                     on u.user_id = ug.user_id)
     where rnk = 1;
    

    应该可以。这是sqlfiddle

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-29
      • 2021-12-14
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多