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

LVL 1
Jason JonesGIS/APP DevAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA EngineerCommented:
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.
Scott PletcherSenior DBACommented:
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.
Jason JonesGIS/APP DevAuthor Commented:
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!
Scott PletcherSenior DBACommented:
SELECT
    CAST(SUM([Data Value]) AS decimal(10, 4)) AS Data_Value,
    'Week of ' + CONVERT(varchar(10), DATEADD(DAY, DATEDIFF(DAY, 6, DateAdd(HOUR, -8, [Date/Time])) / 7 * 7, 6), 101) AS Date_Time
FROM Precipitation_Backup
GROUP BY DATEADD(DAY, DATEDIFF(DAY, 6, DateAdd(HOUR, -8, [Date/Time])) / 7 * 7, 6)
ORDER BY DATEADD(DAY, DATEDIFF(DAY, 6, DateAdd(HOUR, -8, [Date/Time])) / 7 * 7, 6)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jason JonesGIS/APP DevAuthor Commented:
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.