Show counts for all months up to current month including zero counts
up vote
-1
down vote
favorite
My current query which is based on retrieving data for the year 2018:
SELECT DATENAME(MONTH, STARTDATE) AS MonthName, COUNT (*) AS TOTAL
FROM TABLEUSERS
WHERE YEAR(STARTDATE) = '2018'
GROUP BY MONTH(STARTDATE), DATENAME(MONTH,STARTDATE)
The result:
MonthName | TOTAL
January | 25
February | 20
March | 40
April | 11
May | 30
June | 0
July | 0
August | 0
September | 7
October | 55
November | 13
December | 0
As you can see it's retrieving December for the year 2018 on the list and I don't want December to be shown, I am only interested to view data for all the months we've been through (November as of now).
The result must also contain months where the count is zero.
sql sql-server date sql-server-2008 datetime
add a comment |
up vote
-1
down vote
favorite
My current query which is based on retrieving data for the year 2018:
SELECT DATENAME(MONTH, STARTDATE) AS MonthName, COUNT (*) AS TOTAL
FROM TABLEUSERS
WHERE YEAR(STARTDATE) = '2018'
GROUP BY MONTH(STARTDATE), DATENAME(MONTH,STARTDATE)
The result:
MonthName | TOTAL
January | 25
February | 20
March | 40
April | 11
May | 30
June | 0
July | 0
August | 0
September | 7
October | 55
November | 13
December | 0
As you can see it's retrieving December for the year 2018 on the list and I don't want December to be shown, I am only interested to view data for all the months we've been through (November as of now).
The result must also contain months where the count is zero.
sql sql-server date sql-server-2008 datetime
2
Then add a conditionAND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 at 9:03
That query does not produce that result. So I'm not sure what you're asking for here.
– Damien_The_Unbeliever
Nov 22 at 9:08
@Squirrel This works fine, but it actually removes the months in between with zero counts too, how can I only do it with the upcoming month
– MishMish
Nov 22 at 9:16
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
My current query which is based on retrieving data for the year 2018:
SELECT DATENAME(MONTH, STARTDATE) AS MonthName, COUNT (*) AS TOTAL
FROM TABLEUSERS
WHERE YEAR(STARTDATE) = '2018'
GROUP BY MONTH(STARTDATE), DATENAME(MONTH,STARTDATE)
The result:
MonthName | TOTAL
January | 25
February | 20
March | 40
April | 11
May | 30
June | 0
July | 0
August | 0
September | 7
October | 55
November | 13
December | 0
As you can see it's retrieving December for the year 2018 on the list and I don't want December to be shown, I am only interested to view data for all the months we've been through (November as of now).
The result must also contain months where the count is zero.
sql sql-server date sql-server-2008 datetime
My current query which is based on retrieving data for the year 2018:
SELECT DATENAME(MONTH, STARTDATE) AS MonthName, COUNT (*) AS TOTAL
FROM TABLEUSERS
WHERE YEAR(STARTDATE) = '2018'
GROUP BY MONTH(STARTDATE), DATENAME(MONTH,STARTDATE)
The result:
MonthName | TOTAL
January | 25
February | 20
March | 40
April | 11
May | 30
June | 0
July | 0
August | 0
September | 7
October | 55
November | 13
December | 0
As you can see it's retrieving December for the year 2018 on the list and I don't want December to be shown, I am only interested to view data for all the months we've been through (November as of now).
The result must also contain months where the count is zero.
sql sql-server date sql-server-2008 datetime
sql sql-server date sql-server-2008 datetime
edited Nov 22 at 12:43
Salman A
173k66331419
173k66331419
asked Nov 22 at 9:01
MishMish
317313
317313
2
Then add a conditionAND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 at 9:03
That query does not produce that result. So I'm not sure what you're asking for here.
– Damien_The_Unbeliever
Nov 22 at 9:08
@Squirrel This works fine, but it actually removes the months in between with zero counts too, how can I only do it with the upcoming month
– MishMish
Nov 22 at 9:16
add a comment |
2
Then add a conditionAND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 at 9:03
That query does not produce that result. So I'm not sure what you're asking for here.
– Damien_The_Unbeliever
Nov 22 at 9:08
@Squirrel This works fine, but it actually removes the months in between with zero counts too, how can I only do it with the upcoming month
– MishMish
Nov 22 at 9:16
2
2
Then add a condition
AND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 at 9:03
Then add a condition
AND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 at 9:03
That query does not produce that result. So I'm not sure what you're asking for here.
– Damien_The_Unbeliever
Nov 22 at 9:08
That query does not produce that result. So I'm not sure what you're asking for here.
– Damien_The_Unbeliever
Nov 22 at 9:08
@Squirrel This works fine, but it actually removes the months in between with zero counts too, how can I only do it with the upcoming month
– MishMish
Nov 22 at 9:16
@Squirrel This works fine, but it actually removes the months in between with zero counts too, how can I only do it with the upcoming month
– MishMish
Nov 22 at 9:16
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
In SQL Server 2008 (I think) you can use the following:
SELECT DATENAME(MONTH, d) AS MonthName, COUNT(STARTDATE) AS Total
FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11)) AS numbers(n)
CROSS APPLY (SELECT DATEADD(MONTH, n, '2018-01-01')) AS dates(d)
LEFT JOIN TABLEUSERS ON YEAR(STARTDATE) = YEAR(d) AND MONTH(STARTDATE) = MONTH(d)
WHERE n < MONTH(GETDATE())
GROUP BY d
There is a (hard-coded) list of numbers between 0 and 11 that will allow you to generate 12 dates. Rest is straight forward.
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In SQL Server 2008 (I think) you can use the following:
SELECT DATENAME(MONTH, d) AS MonthName, COUNT(STARTDATE) AS Total
FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11)) AS numbers(n)
CROSS APPLY (SELECT DATEADD(MONTH, n, '2018-01-01')) AS dates(d)
LEFT JOIN TABLEUSERS ON YEAR(STARTDATE) = YEAR(d) AND MONTH(STARTDATE) = MONTH(d)
WHERE n < MONTH(GETDATE())
GROUP BY d
There is a (hard-coded) list of numbers between 0 and 11 that will allow you to generate 12 dates. Rest is straight forward.
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
add a comment |
up vote
1
down vote
accepted
In SQL Server 2008 (I think) you can use the following:
SELECT DATENAME(MONTH, d) AS MonthName, COUNT(STARTDATE) AS Total
FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11)) AS numbers(n)
CROSS APPLY (SELECT DATEADD(MONTH, n, '2018-01-01')) AS dates(d)
LEFT JOIN TABLEUSERS ON YEAR(STARTDATE) = YEAR(d) AND MONTH(STARTDATE) = MONTH(d)
WHERE n < MONTH(GETDATE())
GROUP BY d
There is a (hard-coded) list of numbers between 0 and 11 that will allow you to generate 12 dates. Rest is straight forward.
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In SQL Server 2008 (I think) you can use the following:
SELECT DATENAME(MONTH, d) AS MonthName, COUNT(STARTDATE) AS Total
FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11)) AS numbers(n)
CROSS APPLY (SELECT DATEADD(MONTH, n, '2018-01-01')) AS dates(d)
LEFT JOIN TABLEUSERS ON YEAR(STARTDATE) = YEAR(d) AND MONTH(STARTDATE) = MONTH(d)
WHERE n < MONTH(GETDATE())
GROUP BY d
There is a (hard-coded) list of numbers between 0 and 11 that will allow you to generate 12 dates. Rest is straight forward.
In SQL Server 2008 (I think) you can use the following:
SELECT DATENAME(MONTH, d) AS MonthName, COUNT(STARTDATE) AS Total
FROM (VALUES (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11)) AS numbers(n)
CROSS APPLY (SELECT DATEADD(MONTH, n, '2018-01-01')) AS dates(d)
LEFT JOIN TABLEUSERS ON YEAR(STARTDATE) = YEAR(d) AND MONTH(STARTDATE) = MONTH(d)
WHERE n < MONTH(GETDATE())
GROUP BY d
There is a (hard-coded) list of numbers between 0 and 11 that will allow you to generate 12 dates. Rest is straight forward.
edited Nov 22 at 10:13
answered Nov 22 at 9:25
Salman A
173k66331419
173k66331419
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
add a comment |
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 at 9:28
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@MishMish - then specify which version of SQL Server you're using. Since it's been in there since the 2012 version.
– Damien_The_Unbeliever
Nov 22 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
@SalmanA Yes 2008
– MishMish
Nov 22 at 9:35
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53427192%2fshow-counts-for-all-months-up-to-current-month-including-zero-counts%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Then add a condition
AND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 at 9:03
That query does not produce that result. So I'm not sure what you're asking for here.
– Damien_The_Unbeliever
Nov 22 at 9:08
@Squirrel This works fine, but it actually removes the months in between with zero counts too, how can I only do it with the upcoming month
– MishMish
Nov 22 at 9:16