Show counts for all months up to current month including zero counts
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
add a comment |
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
2
Then add a conditionAND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 '18 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 '18 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 '18 at 9:16
add a comment |
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
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
edited Nov 22 '18 at 12:43
Salman A
184k67342441
184k67342441
asked Nov 22 '18 at 9:01
MishMishMishMish
317313
317313
2
Then add a conditionAND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 '18 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 '18 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 '18 at 9:16
add a comment |
2
Then add a conditionAND MONTH(STARTDATE) <= MONTH(GETDATE())
– Squirrel
Nov 22 '18 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 '18 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 '18 at 9:16
2
2
Then add a condition
AND MONTH(STARTDATE) <= MONTH(GETDATE())– Squirrel
Nov 22 '18 at 9:03
Then add a condition
AND MONTH(STARTDATE) <= MONTH(GETDATE())– Squirrel
Nov 22 '18 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 '18 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 '18 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 '18 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 '18 at 9:16
add a comment |
1 Answer
1
active
oldest
votes
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 '18 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 '18 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 '18 at 9:35
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '18 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 '18 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 '18 at 9:35
add a comment |
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 '18 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 '18 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 '18 at 9:35
add a comment |
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 '18 at 10:13
answered Nov 22 '18 at 9:25
Salman ASalman A
184k67342441
184k67342441
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 '18 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 '18 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 '18 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 '18 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 '18 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 '18 at 9:35
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 '18 at 9:28
I get this error: 'EOMONTH' is not a recognized built-in function name. Im using mSSQL
– MishMish
Nov 22 '18 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 '18 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 '18 at 9:29
@SalmanA Yes 2008
– MishMish
Nov 22 '18 at 9:35
@SalmanA Yes 2008
– MishMish
Nov 22 '18 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.
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 '18 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 '18 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 '18 at 9:16