对不起伙计们,我对此感到很疯狂......
我的表:
ID Name Surname Capital Capital_Group Job Job_Group
---------- -------- ----------- ------------- ------ --------------
1 Michael Jackson LessThan50 Entertainer
1 Michael Jackson Medium Entertainer
2 John Lennon Small Swimmer
3 Clara Clinton Huge Runner
3 Clara Clinton Huge Sportsmen
我只想从每个ID 中获取最高行,但不是基于任何内容,除非它出现在其余之上(它已经排序)。任何有帮助的帮助,我的理智都会受到威胁。
答案 0 :(得分:2)
假设您的表按Capital
按降序排列,每个id
且id
定义了一个组,以下内容可能会按您的要求执行:
select t.*
from mytable as t
where not exists (select 1
from mytable as t2
where t2.id = t.id and t2.capital > t.capital
);
答案 1 :(得分:1)
SELECT t.*
FROM mytable AS t
WHERE t.capital = (SELECT MAX(capital)
FROM mytable t2
WHERE t2.id = t.id)
顺便说一句,当有两个人具有相同的身份和资本时,你想做什么?
答案 2 :(得分:0)
with cte
(
select *, row_Number() over(Partition by ID order by Name ) as RowNumber
)
select * from cte
where RowNumber=1
试试这个,让我知道你对此的评论。
答案 3 :(得分:0)
select distinct ID ,Name ,Surname,Capital from mytable order by ID
答案 4 :(得分:0)
SELECT *
FROM yourTable
GROUP BY id
HAVING MAX(Capital)
答案 5 :(得分:0)
将#,row_number()over(按ID排序)作为RNO选择到#temp1
选择*从#temp为t,其中RNO不在(从#temp中选择max(RNO)作为tt group by ID)
我认为它会对你有所帮助