我有一个表测试如下: test1
Name NumbreMission
John 1
John 2
John 3
Jack 1
Jack 2
Jack 3
Jack 4
Jack 5
我希望得到以下结果:select max(NumbreMission)
Name NumbreMission
John
John
John 3
Jack
Jack
Jack
Jack
Jack 5
有什么想法吗? 任何帮助都会受到最多的赞赏吗?
答案 0 :(得分:2)
rank()
函数应该可以解决这个问题,但需要进行一些操作:
SELECT Name,
CASE WHEN rk = 1
THEN NumberMission
ELSE NULL
END AS NumberMission
FROM (SELECT Name,
NumberMisson,
RANK() OVER(PARTITION BY Name ORDER BY NumberMission DESC) AS rk
FROM test1) t
答案 1 :(得分:2)
您可以使用CTE获取每个名称的最大值,然后将其连接到原始表:
with CTE as
(select
name,
max(numbremission) as maxnum
from
test1
group by name)
select
t1.name,
cte.maxnum
from
test1 t1
left outer join cte
on t1.name = cte.name
and t1.numbremission = cte.maxnum