我有一个查询的以下结果,我想要做一个子查询,计算每个名称中最小值的数目。
例如:
Marcus 25
Marcus 27
Marcus 25
Jonathan 36
Jonathan 36
Jonathan 36
Jonathan 38 其结果应该是:
Marcus 25 2
Jonathan 36 3有什么想法吗?
发布于 2017-10-01 14:12:50
一种方法使用窗口函数:
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;https://stackoverflow.com/questions/46513205
复制相似问题