Skip to content
Advertisement

List count for last 12 months broken down by month

I have a query that can get a total active count of products until a specified date @POINT

Output for @POINT "01/01/2021" is

From a table like

How would I remake this query so that I can have a list of points (last 12 months) like

in one query? I don’t want to create a separate table of dates. The database is MSSQL.


Since no responded to question, I’ll assume there isn’t a function to generate these dates efficiently. I wrote a subquery that CAST couple dates to varchar to date, resulting with first months for the past 12 months.

Advertisement

Answer

Just group it by the Point and COUNT(*) the result.

You say in your update “CAST couple dates to varchar to date”, which I think means you want just the date part, in which case you can use CAST(NEXT.date AS date):

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement