How can I create a row for values that don't exist and fill the count with 0 values?

前端 未结 4 1305
北海茫月
北海茫月 2021-01-26 10:23

In SQL Server, I\'m running a query on users age groups on data where, for some years, there are zero users per age group. For example there were users in 2013 in the \"18-21\"

4条回答
  •  旧时难觅i
    2021-01-26 10:45

    You do this by joining with a "fake" list of age group+years:

    SELECT AGE_GROUPS.YEAR, AGE_GROUPS.AGE_GROUP, COALESCE(SUM(USERS), 0) as usercount
    FROM (
        SELECT YEAR, AGE_GROUP
        FROM (
            SELECT '18-21' AS AGE_GROUP
            UNION SELECT '22-25'
            UNION SELECT '25-28'
        ) AGE_GROUPS, (SELECT DISTINCT YEAR FROM USERS) YEARS
    ) AGE_GROUPS
    LEFT JOIN USERS ON (USERS.AGE_GROUP = AGE_GROUPS.AGE_GROUP AND USERS.YEAR = AGE_GROUPS.YEAR)
    WHERE AGE_GROUPS.YEAR = '2014'
    GROUP BY AGE_GROUPS.YEAR, AGE_GROUPS.AGE_GROUP
    

    You can also simplify this, assuming that your USERS table has all possible age groups ignoring a specific year:

    SELECT AGE_GROUPS.YEAR, AGE_GROUPS.AGE_GROUP, COALESCE(SUM(USERS), 0) as usercount
    FROM (
        SELECT YEAR, AGE_GROUP
        FROM (SELECT DISTINCT AGE_GROUP FROM USERS) AGE_GROUPS, (SELECT DISTINCT YEAR FROM USERS) YEARS
    ) AGE_GROUPS
    LEFT JOIN USERS ON (USERS.AGE_GROUP = AGE_GROUPS.AGE_GROUP AND USERS.YEAR = AGE_GROUPS.YEAR)
    WHERE AGE_GROUPS.YEAR = '2014'
    GROUP BY AGE_GROUPS.YEAR, AGE_GROUPS.AGE_GROUP
    

提交回复
热议问题