Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have query which returns hourly data. But I want to get daily data from this query, so all the hourly data per day would be averaged to daily data.

What I have tried:

declare @Days int

set @Days = -1

select      

dateadd(hour,datepart(hour,Timestamp),cast(CAST((Timestamp) as date) as datetime)) as [Time]  
,[Value]    

from  [Employee]

where dateadd(hour,datepart(hour,Timestamp),cast(CAST((Timestamp) as date) as datetime)) >= CONVERT(date, DATEADD(DAY, @Days, GETDATE()))
Posted
Updated 26-Jul-18 9:22am
v3
Comments
[no name] 26-Jul-18 4:51am    
And, so? It "looks" like it might work.
Member 13863605 26-Jul-18 14:38pm    
no this gives me hourly data not daily
Tomas Takac 26-Jul-18 6:05am    
I believe you are looking for GROUP BY[^].
Member 13863605 26-Jul-18 15:23pm    
yes. I have provided my answer below

1 solution

DECLARE @Days int = -1;
SELECT 
      CAST(Timestamp AS date) AS date
    , AVG(Value) AS Value
FROM  [Employee]
WHERE Timestamp >= DATEADD(day, @Days, CAST(GETDATE() AS date))
GROUP BY CAST(Timestamp AS date)
ORDER BY date;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900