gpt4 book ai didi

sql - SQL Server 分区计数中的计数

转载 作者:行者123 更新时间:2023-12-02 19:05:17 24 4
gpt4 key购买 nike

SELECT MobilePhone, COUNT(MobilePhone) OVER(PARTITION BY MobilePhone) AS CountMobilePhone
FROM ES_TABLE
WHERE applic IN ( SELECT appl
FROM Campaign JOIN Client ON Campaign.ClientID = Client.ClientID
WHERE Client.ClientCode = 'OPIS')

这就是我的查询

输出为

MobilePhone  | CountMobilePhone
121928 | 1
912819 | 1
129819 | 3
198219 | 5
918298 | 5

我想按 CountMobilePhone 分组进行计数。就这样

Count   | CountMobilePhone
2 | 1
1 | 3
2 | 5

这可能吗?我已经尝试了几次但出现错误

最佳答案

只需对计数本身进行另一个聚合即可:

SELECT CountMobilePhone, COUNT(*) AS cnt
FROM
(
SELECT COUNT(MobilePhone) OVER(PARTITION BY MobilePhone) AS CountMobilePhone
FROM ES_TABLE
WHERE applic IN (SELECT appl FROM Campaign INNER JOIN Client
ON Campaign.ClientID = Client.ClientID
WHERE Client.ClientCode = 'OPIS')
) t
GROUP BY CountMobilePhone
ORDER BY CountMobilePhone;

关于sql - SQL Server 分区计数中的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47789349/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com