Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Incorrect totals when summing counts for past 12 month Expand / Collapse
Author
Message
Posted Saturday, July 6, 2013 2:08 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: 2 days ago @ 1:59 PM
Points: 3, Visits: 32
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!
Post #1470901
Posted Saturday, July 6, 2013 2:58 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, September 15, 2014 4:57 AM
Points: 802, Visits: 717
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())) 



Erland Sommarskog, SQL Server MVP, www.sommarskog.se
Post #1470903
Posted Saturday, July 6, 2013 2:15 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: 2 days ago @ 1:59 PM
Points: 3, Visits: 32
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.

Post #1470938
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse