sql - 取消率最高的输出月份
问题描述
我可以使用以下查询获取自 2018 年以来每个月取消会员的数量 -
SELECT DATEPART(YEAR, Cancel) [Year],
DATEPART(Month, Cancel) [Month], COUNT(1) [Count]
FROM Subscription
where DATEPART(YEAR, Cancel) >= 2018
GROUP BY DATEPART(year, Cancel),DATEPART(Month, Cancel)
ORDER BY 1,2
输出是
Year Month Count
2018 1 6416775
2018 2 458171
2018 3 29963575
2018 4 65393505
如何修改此查询,使其仅显示取消次数最多的月份?
更新
这是我想要的输出。每一年都应该有最高的Count
输出值。
Year Month Count
2018 4 65393505
2019 2 458171
输出 2018 年的第 4 个月,因为它与前 4 个月相比具有最高值。
解决方案
使用order by count desc
并选择第一条记录top 1
可以获得所需的输出:
SELECT TOP 1 DATEPART(YEAR, Cancel) [Year],
DATEPART(Month, Cancel) [Month], COUNT(1) [Count]
FROM Subscription
where DATEPART(YEAR, Cancel) >= 2018
GROUP BY DATEPART(year, Cancel),DATEPART(Month, Cancel)
ORDER BY Count DESC
推荐阅读
- prometheus - Prometheus 节点导出器未报告非 root 拥有的挂载
- keras - 为什么我的keras模型预测的概率总是零
- ansible - Ansible:使用 JMESPath 过滤包含字符串的元素
- sql-server - SQL Server 中浮点计算的结果不一致
- typescript - 如何从打字稿中的对象文字类型中提取一种类型?
- gradle - 当构建任务已经存在时,为模块构建任务附加dependsOn
- hyperledger-fabric - 更新或替换超级账本 fabrc 身份的最佳方法是什么
- django - 使用 django-mssql-backend 的数据库错误连接
- php - 如何从默认控制器读取 URL 参数
- python - 在 python 中使用正则表达式从段落块中获取所需的字段值