[英]SELECT TOP from each group
對不起伙計們,我對此感到很瘋狂......
我的桌子:
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中獲得最高行, 但不是基於任何東西,除了它出現在其余 ID 之上 (它已經排序)。 任何有幫助的幫助,我的理智都會受到威脅。
假設您的表按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
);
SELECT t.*
FROM mytable AS t
WHERE t.capital = (SELECT MAX(capital)
FROM mytable t2
WHERE t2.id = t.id)
順便說一句,當有兩個人具有相同的身份和資本時你想做什么?
with cte
(
select *, row_Number() over(Partition by ID order by Name ) as RowNumber
)
select * from cte
where RowNumber=1
試試這個,讓我知道你對此的評論。
select distinct ID ,Name ,Surname,Capital from mytable order by ID
SELECT *
FROM yourTable
GROUP BY id
HAVING MAX(Capital)
選擇*,row_number()over(按ID排序)作為RNO到#temp1
select * from #temp as t其中RNO不在(從#temp中選擇max(RNO)作為tt group by ID)
我認為它會對你有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.