本文介紹了根據(jù)一列的最小值選擇行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我需要在 MSSQL DB 中選擇 A 列具有不同值且 Order 列具有最小值的行.
I need to select row with distinct value of column A and with minimal value of Order column in MSSQL DB.
每條記錄的右側(cè)都有更多列.我知道我應(yīng)該使用 GROUP BY,但是當我想保留右側(cè)列時它確實會發(fā)出警告.
Each record has more columns on the right hand side. I know I should used GROUP BY, but it does throw warnings, when I want to keep the right hand side columns a well.
數(shù)據(jù)集示例:
A | Order | Multiple columns ... |
--------+-------+-------------------------+
A1 | 3 | ... |
A1 | 7 | ... |
A2 | 2 | ... |
A3 | 2 | ... |
A3 | 8 | ... |
所以我想得到這些結(jié)果:
So that I want to get these results:
A | Order | Multiple columns ... |
--------+-------+-------------------------+
A1 | 3 | ... |
A2 | 2 | ... |
A3 | 2 | ... |
我嘗試使用并拋出警告的查詢是這樣的:
The query I tried to use and throws warning is this one:
SELECT A, MIN(Order), Column B, Column C, Column D...
FROM Table
GROUP BY A
ORDER BY A
推薦答案
你也可以使用 top (1) with ties
select top (1) with ties a, *
from table t
order by row_number() over (partition by a order by [Order])
這篇關(guān)于根據(jù)一列的最小值選擇行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!