Show counts for all months up to current month including zero counts












-1















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.










share|improve this question




















  • 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
















-1















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.










share|improve this question




















  • 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














-1












-1








-1








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.










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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














  • 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








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












1 Answer
1






active

oldest

votes


















1














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.






share|improve this answer


























  • 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











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
});


}
});














draft saved

draft discarded


















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









1














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.






share|improve this answer


























  • 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
















1














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.






share|improve this answer


























  • 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














1












1








1







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








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



















  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings