我可以使用以下查询获取自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个月相比,它具有最高的价值。
答案 0 :(得分:2)
使用let array = ["one","two","three"]
let string = "one two three four five six seven"
并通过order by count desc
选择第一条记录,您将获得所需的输出:
top 1
答案 1 :(得分:0)
使用row_number()
with cte as
(
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
)
,cte1 as
( select *,ROW_NUMBER() OVER( partition by [Year] ORDER BY [Count] DESC) rn from cte
) select * from cte1 where rn=1
答案 2 :(得分:0)
正在关注
SELECT *
FROM (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) as Table1
WHERE Count = (SELECT MAX COUNT(1) [Count]
FROM Subscription
WHERE DATEPART(YEAR, Cancel) >= 2018
GROUP BY DATEPART(year, Cancel),DATEPART(Month, Cancel)
ORDER BY 1,2)