确定拥有最多年任期的总统的姓名,政党和年数 派对。我无法使用join关键字。
表总裁:
Name | Party | years_served|
-------------+-------------+-------------+
Roosevelt | Democratic | 12
Madison | Demo-rep | 8
Monroe | Demo-rep | 8
Adams | Demo-rep | 4
结果将是罗斯福,麦迪逊和梦露出现,亚当斯没有,因为他没有服务8年,这是演示代表的最大值。
答案 0 :(得分:1)
您可以使用dense_rank()
窗口功能:
with cte as (
select
*, dense_rank() over(partition by Party order by years_served desc) as rnk
from Table1
)
select
Name, Party, years_served
from cte
where rnk = 1
<强> sql fiddle demo 强>
答案 1 :(得分:0)
试试这个:
SELECT Name
FROM President pr
INNER JOIN (SELECT Party, MAX(years_served) as YS FROM president GROUP BY Party) as MaxYS on MaxYS.Party=Pr.Party
WHERE pr.years_served>=MaxYS.YS
希望就是你要找的。 p>