【问题标题】:Getting top 1 value for each month每个月获得前 1 个值
【发布时间】:2021-10-01 05:06:41
【问题描述】:

这是代码:

select date_part('month',inspection.idate) as _month, inspector.iname, count(inspector.iname) as num
from inspector,inspection 
where inspection.idate>='2021/1/1' and inspector.iid = inspection.iid
group by inspector.iname, _month
order by _month

结果如下: enter image description here

需要显示每个月的前 1 个计数,对于第 6 个月,有 2 个计数相同,需要同时显示。

【问题讨论】:

标签: postgresql greatest-n-per-group


【解决方案1】:

您可以使用排名函数来解决此问题。可以是 DenseRank 或 Rank。

【讨论】:

    猜你喜欢
    • 2023-03-03
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2020-09-25
    相关资源
    最近更新 更多