0

If I have the table "members" (shown below), how would I go about getting the record of the first occurrence of a membership_id (Oracle).

Expected results

123                 John                      Doe                      A       P
313                 Michael                   Casey                    A       A
113                 Luke                      Skywalker                A       P

Table - members

membership_id       first_name                last_name                status  type
123                 John                      Doe                      A       P
313                 Michael                   Casey                    A       A
113                 Luke                      Skywalker                A       P
123                 Bob                       Dole                     A       A
313                 Lucas                     Smith                    A       A
4

3 回答 3

3
SELECT membership_id,
       first_name,
       last_name,
       status,
       type
  FROM( SELECT membership_id,
               first_name,
               last_name,
               status,
               type,
               rank() over (partition by membership_id
                                order by type desc) rnk
          FROM members )
 WHERE rnk = 1

将适用于您的样本数据集。如果您可以有联系——即具有相同membership_id和相同最大值的多行type——此查询将返回所有这些行。如果您只想返回有平局的行之一,您需要向 中添加额外的条件以order by确保所有平局都被打破,或者您需要使用该row_number函数而不是rank任意打破平局。

于 2012-09-18T15:15:18.510 回答
0
select *
from members 
where rowid in (
    select min(rowid)
    from members
    group by membership_id
)
于 2012-09-19T05:31:24.367 回答
0
Select A.*
FROM Members AS A inner join
     (Select membership_id, first(first_name) AS FN, first(last_name) AS LN 
      From Members
      Group by membership_id) AS B
     ON A.membership_id=B.membership_id and A.first_name=B.FN and A.last_name=B.LN

希望有帮助!

于 2012-09-18T15:06:11.550 回答