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

前端 未结 4 1301
北海茫月
北海茫月 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条回答
  • 2021-01-26 10:37

    I assume you have another table that contain all rows Age Group.

    TABLE NAME: AGEGROUPS

    AGE_GROUP
    18-21
    22-25
    25-28
    

    Try this:

    SELECT '2014' AS YEAR, AG.AGE_GROUP, COALESCE(TB.usercount, 0) AS usercount
    FROM (
        SELECT YEAR, AGE_GROUP, SUM(USERS) as usercount,
        FROM USERS
        WHERE YEAR = '2014'
        AND PRIMARY_GROUP = 'NT'
        GROUP BY YEAR, AGE_GROUP
    ) AS TB
    RIGHT JOIN AGEGROUPS AG ON TB.AGE_GROUP=AG.AGE_GROUP
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-26 10:51

    Try This...

    SELECT YEAR, AGE_GROUP, isnull(Sum(USERS),0) as usercount,
    FROM USERS
     WHERE YEAR = '2013'
    AND PRIMARY_GROUP = 'NT'
    GROUP BY YEAR, AGE_GROUP
    
    0 讨论(0)
  • 2021-01-26 10:59

    Assuming that all the years and age groups are in the table (some row for the table), then you can do this just with this table. The idea is to generate all the rows using a cross join and then use a left join to bring in the values you want:

    select y.year, ag.age_group, count(u.year) as usercount
    from (select 2013 as year) y cross join
         (select distinct age_group from users) ag left join
         users u
         on u.year = y.year and u.age_group = ag.age_group and
            u.primary_group = 'NT'
    group by y.year, ag.age_group;
    

    I don't know what sum(users) is supposed to be. If you do indeed have a column users.users, then use it with sum(). It looks like you really want count().

    0 讨论(0)
提交回复
热议问题