2017-12-13 3 views
4
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') 

das ist meine AbfrageCOUNT in COUNT OVER PARTITION SQL Server

die Ausgabe

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

I Gruppe für das Zählen von CountMobilePhone wollen. so zu sein

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

ist das möglich? Ich habe mehrmals versucht, aber immer Fehler

Antwort

3

Sie einfach eine andere Aggregation auf den Grafen ersetzt selbst:

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; 
+0

Danke, es funktioniert – TARA

2

versuchen diese

WITH CTE 
AS 
(

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

) 
SELECT 
    [Count] = COUNT(1), 
    CountMobilePhone 
    FROM CTE 
     GROUP BY CountMobilePhone 

Ich habe auch die IN mit existiert für eine bessere Leistung

+0

danke es funktioniert. aber ich sehe Antwort von Tie Biegelsien zuerst. aber ich stimme für dich – TARA

+0

Eine andere Verbesserung, die du machen könntest, wäre, 'MobilePhone' im CTE nicht zu wählen, da es nicht wirklich benötigt wird, um die Zählungen zu zählen. –

+0

ist es in Ordnung. wir müssen das Problem gelöst mit dem besten Weg das ist alles –