Incorrect totals when summing counts for past 12 month

  • Hello everyone,

    I have run into an issue where the query below returns monthly totals that include the counts with "lastupdatedate" on the 1st day of the next month as well. In other words, counts in April 2013 also include records with "lastupdatedate" value of 05/01/2013 as well. I can't seem to find a solution to this one. Any help is appreciated!

    The data type of the "lastupdatedate" column is datetime.

    SELECT DATEDIFF(SECOND,{d '1970-01-01'}, MIN(ins.lastUpdateDate)) AS Ids,

    CASE

    WHEN MONTH(ins.lastUpdateDate) = 1 THEN 'Jan, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 2 THEN 'Feb, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 3 THEN 'Mar, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 4 THEN 'Apr, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 5 THEN 'May, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 6 THEN 'Jun, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 7 THEN 'Jul, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 8 THEN 'Aug, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 9 THEN 'Sep, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 10 THEN 'Oct, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 11 THEN 'Nov, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    WHEN MONTH(ins.lastUpdateDate) = 12 THEN 'Dec, ' + CAST(YEAR(ins.lastUpdateDate) as nvarchar(4))

    END AS Groups, COUNT(ins.Id) AS Counts

    FROM tblInsp ins

    WHERE ins.custID = 1234

    and ins.lastUpdateDate > Dateadd(year,-1,getdate())

    GROUP BY MONTH(ins.lastUpdateDate), YEAR(ins.lastUpdateDate)

    Thanks a bunch!

  • You could do away with the big CASE expression if you use the datename function:

    datename(MONTH, ins.lasUpdateDate) + ', ' + datename(YEAR, ins.lastUpdateDate)

    As for the actual problem, I assume that ins.LastUpdateDate includes a time portion, why the condition

    and ins.lastUpdateDate > Dateadd(year,-1,getdate())

    should be

    and ins.lastUpdateDate > Dateadd(year,-1, convert(date, getdate()))

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Thank you for the suggestion about the Case statement Erland. Regarding my actual problem, it turns out that both approaches (my original and your suggested) return identical results.

    I investigated the issue further and it turned out that the results were correct and the counts returned did not have overlapping dates. I originally had used another sproc to validate the counts instead of the actual data and it turned out that the sproc I had used was not being called properly.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply