Link to home
Start Free TrialLog in
Avatar of Kristen Jones
Kristen JonesFlag for United States of America

asked on

MS SQL Query Grouping Data by week

I am trying to fix a query that was written a long time ago by another developer that we discovered recently was not working.

So data background information. There is a table called Precipitation_Backup and it has the following fields:

[Sensor Id] (Int)
[Data Type] (Text)
[Data Value] (Int)
[Date/Time] (date/time field)

Basically the Query is used in an app that passes the Sensor Id and the Start and end date to query the records.  

It is supposed to add up all of the [Data Value] 's for each week. Where the Day of each week STARTS at 8;00AM from the previous DAY. The problem right now is the recordset is empty when there is valid data.  

Here is the current Query:


SELECT  CAST(SUM([Data Value]) AS decimal

(10, 4)) AS Data_Value,
 
('Week of ' + WeekFirstDate) as Date_Time From (Select [Data Value], 

( Case datepart(dw,DateAdd(hh, -8, [Date/Time])) 

when 1 then CONVERT(varchar, DateAdd(hh, -8, [Date/Time]), 101) 

when 2 then CONVERT(varchar, DateAdd(dd,-1,DateAdd(hh, -8, [Date/Time])), 101) 

when 3 then CONVERT(varchar, DateAdd(dd,-2,DateAdd(hh, -8, [Date/Time])), 101) 

when 4 then CONVERT(varchar, DateAdd(dd,-3,DateAdd(hh, -8, [Date/Time])), 101) 

when 5 then CONVERT(varchar, DateAdd(dd,-4,DateAdd(hh, -8, [Date/Time])), 101) 

when 6 then CONVERT(varchar, DateAdd(dd,-5,DateAdd(hh, -8, [Date/Time])), 101) 

when 7 then CONVERT(varchar, DateAdd(dd,-6,DateAdd(hh, -8, [Date/Time])), 101) end ) as WeekFirstDate 

FROM Precipitation_Backup 

WHERE ([Sensor Id] = '4980') AND ([Data Type] = 'S') AND ([Date/Time] > '01/01/2013 8:00:00 AM') AND ([Date/Time] < '4/15/2015 8:00:00 AM')) A 

Group By ('Week of ' + WeekFirstDate), Convert(varchar, Convert(datetime, WeekFirstDate), 112) 

Order By Convert(varchar, Convert(datetime, WeekFirstDate), 112)

Open in new window

Avatar of Mark Bullock
Mark Bullock
Flag of United States of America image

The columns in the GROUP BY clause should be the same as the columns in the SELECT clause. The SELECT clause can have additional aggregate values like SUM.

The second column in the GROUP BY clause is not present in the SELECT clause. Try removing it.
What day of the week does the week start on?  The posted code is ambiguous, because it depends on the setting of DATEFIRST.  My code won't, but I need to know the correct day of the week to write it for.
Avatar of Kristen Jones

ASKER

Mark:  Actually the Select and Group match it may be hard to read because I put them in lines but they both have Data_Value and Date_Time.  The query does not error if they didn't match there would be an error.

Scott: The day of the week would start on Sunday however Sunday's data values Start the Daily SUM Saturday AFTER 8AM to 8AM Sunday  So The start of the week is SUNDAY (The values for Sunday are from 8AM Saturday to 8AM Sunday) Then Monday's Values would be from Sunday at 8Am to Monday at 8AM.  The Week then would be from Saturday AFTER 8AM to the next Saturday at 8AM.  

Thanks for the help guys!
ASKER CERTIFIED SOLUTION
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Pretty Impressive!  Thanks Scott it seems to work. It may take a while to check the data manually but I get results, so I'll award points.