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" age group, so the query returns the next age group, "22-25", as the first row because there were no entries containing "18-21." Instead, I would like to return a row that contains 18-21, but has 0 as the value for number of users.
Currently, I have:
SELECT YEAR, AGE_GROUP, SUM(USERS) as usercount,
FROM USERS
WHERE YEAR = '2013'
AND PRIMARY_GROUP = 'NT'
GROUP BY YEAR, AGE_GROUP
This returns:
YEAR AGE_GROUP usercount
2014 22-25 200
2014 25-28 10
I want it to return:
YEAR AGE_GROUP usercount
2014 18-21 0
2014 22-25 200
2014 25-28 10
How can I create a row for specific values that don't exist and fill the count with 0 values?
For the record, I DO in fact have a column called 'users' in the users table. Confusing, I know, but it's a stupidly named schema that I took over. The Users table contains data ABOUT my users for reporting. It should probably have been named something like Users_Reporting.