本文介紹了根據一列的最小值選擇行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我需要在 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.
每條記錄的右側都有更多列.我知道我應該使用 GROUP BY,但是當我想保留右側列時它確實會發出警告.
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.
數據集示例:
A | Order | Multiple columns ... |
--------+-------+-------------------------+
A1 | 3 | ... |
A1 | 7 | ... |
A2 | 2 | ... |
A3 | 2 | ... |
A3 | 8 | ... |
所以我想得到這些結果:
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])
這篇關于根據一列的最小值選擇行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!