[英]SQL Server query count of the minimum value for distinct names in a table
我從查詢中得到以下結果,並且我想做一個子查詢,該子查詢計算每個名稱中最小值的數量。
例如:
Marcus 25
Marcus 27
Marcus 25
Jonathan 36
Jonathan 36
Jonathan 36
Jonathan 38
結果應該是:
Marcus 25 2
Jonathan 36 3
有任何想法嗎?
一種方法使用窗口函數:
select t.name, count(*)
from (select t.*,
rank() over (partition by name order by val) as seqnum
from t
) t
where seqnum = 1;
編輯:
哦,OP還需要該值:
select t.*
from (select t.name, t.val, count(*) as cnt,
row_number() over (partition by name order by val) as seqnum
from t
group by t.name, t.val
) t
where seqnum = 1;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.