Avatar of James Steinbacher
James SteinbacherFlag for United States of America

asked on 

Need to combine hourly and daily average data in SELECT

I have a query that averages hi-resolution sensor data across an hour:

SELECT 
   DATEADD(HOUR, DATEDIFF(HOUR, 0, Sensor_Turb0.t_stamp), 0) AS t_stamp_hour
   , AVG(Sensor_Turb0.mbTurb0_Turb) AS Avg_Turb
   , AVG(Sensor_Flow0.mbflow0_Flow) AS Avg_Flow

FROM Sensor_Turb0
  INNER JOIN Sensor_Flow0 ON Sensor_Turb0.t_stamp = Sensor_Flow0.t_stamp

WHERE (Sensor_Turb0.t_stamp > DATEADD(DAY,-1,CAST(GETDATE() AS date))) 

GROUP BY DATEADD(HOUR, DATEDIFF(HOUR, 0, Sensor_Turb0.t_stamp), 0)
ORDER BY DATEADD(HOUR, DATEDIFF(HOUR, 0, Sensor_Turb0.t_stamp), 0)

Open in new window

The result of the query is used to generate a chart:
User generated image

I need to also include an average line for each pen, resulting in a chart that would look something like this:
User generated image

I can get the average of either chart pen using a query like this:

SELECT 
   AVG(Sensor_Turb0.mbTurb0_Turb) AS Avg_Turb

FROM Sensor_Turb0

WHERE (Sensor_Turb0.t_stamp > DATEADD(DAY,-1,CAST(GETDATE() AS date))) 

Open in new window


Unfortunately, the charting application I need to use doesn't allow multiple data sources (queries).  Is there a way to get both the hourly average data and the daily average data returned from a single query?

Thank you for looking at this issue!
Microsoft SQL Server

Avatar of undefined
Last Comment
PortletPaul
Avatar of Andrei Fomitchev
Andrei Fomitchev
Flag of United States of America image

You can create a table
CREATE TABLE temp (Id Int, Hourly Numeric(14,2), Daily Numeric(14,2))
INSERT INTO temp (Id) VALUE (1)

Schedule SQL Server Job to update the values hourly:
UPDATE temp SET Hourly = (SELECT AVG(...)) WHERE Id = 1
UPDATE temp SET Daily = (SELECT AVG(...)) WHERE Id = 1

In your application use one query:
SELECT Hourly, Daily FROM temp WHERE Id = 1

Instead of Id you can use Hour, Day. In such case the table will have history like:
Day, Hour, Hourly,  Daily
33,      1,      0.456   0.654
33,       2,     0.461   0.654

SELECT Hourly, Daily FROM temp WHERE (Day = 33) AND (Hour BETWEEN 7 AND 17) 


ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo