Mysql
 sql >> Database >  >> RDS >> Mysql

Utilizzo di più gruppi avendo in una singola query

Devi inserire il tuo primo conteggio in una sottoquery:

SELECT  COUNT(CASE WHEN C = 1 THEN 1 END) AS nooffamiliesHavingcount1,
        COUNT(CASE WHEN C = 2 THEN 1 END) AS nooffamiliesHavingcount2
FROM    (   SELECT  COUNT(*) AS C
            FROM    TableName
            GROUP BY House_No
        ) t
WHERE   c IN (1, 2);

MODIFICA

Se hai bisogno di fare intervalli nel tuo conteggio puoi usare questo:

SELECT  COUNT(CASE WHEN C <= 1 THEN 1 END) AS nooffamiliesHavingcount1,
        COUNT(CASE WHEN C BETWEEN 2 AND 4 THEN 1 END) AS nooffamiliesHavingcount2,
        COUNT(CASE WHEN C > 4 THEN 1 END) AS nooffamiliesHavingcount3
FROM    (   SELECT  COUNT(*) AS C
            FROM    TableName
            GROUP BY House_No
        ) t

Esempio su SQL Fiddle