Search code examples
sql-servercrystal-reports

SQL Query to obtain number of tickets in a given month of a given year


Here's what I've got so far:

SELECT ToText("CLSDDATE",'MMMM yyyy'),COUNT("PRIORITY") AS TotalPerMonthLow
FROM "TRACKIT_DATA2"."TrackItApp_1"."vTASKS_BROWSE"
WHERE "PRIORITY" = 'Low - 3 Days'
AND YEAR("CLSDDATE") > '2012'
GROUP BY MONTH("CLSDDATE")

This works (except for the ToText, but the concept is there), but it groups together all of the tickets in all of the January's together. Ideally, this query would return a table that looks like this:

  1. January 2012 - 103
  2. February 2012 - 114
  3. March 2012 - 121

...

  1. January 2015 - 451
  2. February 2015 - 472

etc.

This is going to be used to plot ticket fulfillment trends across our years of operation in a Crystal Report. It will be in a scatter plot, so I supposed the MonthYear column could just be 0-indexed from January 2012.


Solution

  • Repeat the same calculated field in GROUP BY

    SELECT ToText("CLSDDATE",'MMMM yyyy'),COUNT("PRIORITY") AS TotalPerMonthLow
    FROM "TRACKIT_DATA2"."TrackItApp_1"."vTASKS_BROWSE"
    WHERE "PRIORITY" = 'Low - 3 Days'
    AND YEAR("CLSDDATE") > '2012'
    GROUP BY ToText("CLSDDATE",'MMMM yyyy'), MONTH("CLSDDATE")