I have the following result from a query and i want to make a subquery that counts the number of the minimum value in each name.
For example:
Marcus 25
Marcus 27
Marcus 25
Jonathan 36
Jonathan 36
Jonathan 36
Jonathan 38
And the result should be :
Marcus 25 2
Jonathan 36 3
Any ideas?
One method uses window functions:
select t.name, count(*)
from (select t.*,
rank() over (partition by name order by val) as seqnum
from t
) t
where seqnum = 1;
EDIT:
Oh, the OP also wants the value:
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.