如果我有“成员”表(如下所示),我将如何获取第一次出现的membership_id(Oracle)的记录。
预期结果
123 John Doe A P
313 Michael Casey A A
113 Luke Skywalker A P
表 - 成员
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
答案 0 :(得分: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
会随意破坏关系。
答案 1 :(得分: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
希望有所帮助!
答案 2 :(得分:0)
select *
from members
where rowid in (
select min(rowid)
from members
group by membership_id
)