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 12»»

Get sum of averages - a challenge Expand / Collapse
Author
Message
Posted Friday, March 29, 2013 7:41 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 8:56 AM
Points: 139, Visits: 385
I have a need for a query that groups data, returns averages for the groups, but also sums those averages. This has been stumping me...

A simplified version of the table is
CREATE TABLE table_1 (Curr_date DATE, Action CHAR(15), Duration FLOAT)

Data is like
INSERT Table_1 VALUES ('2013-03-29','SignOn',1000.1234);
INSERT Table_1 VALUES ('2013-03-29','GetBal',450.1234);
INSERT Table_1 VALUES ('2013-03-29','SignOn',900.1234);
INSERT Table_1 VALUES ('2013-03-29','GetBal',300.1234);
INSERT Table_1 VALUES ('2013-03-29','SignOn',1100.1234);
INSERT Table_1 VALUES ('2013-03-29','GetBal',475.1234);
INSERT Table_1 VALUES ('2013-03-29','SignOn',950.1234);
INSERT Table_1 VALUES ('2013-03-29','GetBal',320.1234);
INSERT Table_1 VALUES ('2013-03-29','SignOn',800.1234);

I can get the average action durations like this
SELECT Curr_date, Action, AVG(Duration)/1000 as Avg_Duration
FROM Table_1
WHERE Curr_date = CAST(GETDATE() as DATE)
GROUP BY Curr_date, Action

Curr_date Action Avg_Duration
2013-03-29 GetBal 0.3863734
2013-03-29 SignOn 0.9501234


But I also need to sum those averages for both actions together. So for SignOn and GetBal together the sum is about 1.34. Anyone have any good suggestions for that? Thanks!


Post #1436925
Posted Friday, March 29, 2013 7:47 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
Not totally sure how you want the output but this should at least get you started.

;with MyAverage as
(
SELECT Curr_date, Action, AVG(Duration)/1000 as Avg_Duration
FROM Table_1
WHERE Curr_date = CAST(GETDATE() as DATE)
GROUP BY Curr_date, Action
)

select Curr_date, Action, Avg_Duration, (select sum(AVG_Duration) from MyAverage) as SummedAverage
from MyAverage



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436931
Posted Friday, March 29, 2013 7:58 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 8:56 AM
Points: 139, Visits: 385
That's nice and elegant. Thanks!

Starting a sql statement with WITH is new to me. I will have to look into that closer but it works great.
Post #1436940
Posted Friday, March 29, 2013 8:09 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
Ken Davis (3/29/2013)
That's nice and elegant. Thanks!

Starting a sql statement with WITH is new to me. I will have to look into that closer but it works great.


It is a common table expression or CTE. You should certainly read up on them as they are quite useful.

http://msdn.microsoft.com/en-us/library/ms190766%28v=sql.105%29.aspx


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436948
Posted Friday, March 29, 2013 9:13 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 2:22 PM
Points: 2,121, Visits: 3,207
Until you get to SQL 2012, you also have the "quick-and-dirty" version of that, using COMPUTE:

SELECT Curr_date, Action, AVG(Duration)/1000 as Avg_Duration
FROM Table_1
WHERE Curr_date = CAST(GETDATE() as DATE)
GROUP BY Curr_date, Action
-- added AVG just to show that multiple COMPUTE functions can be specified
COMPUTE SUM(AVG(Duration)/1000), AVG(AVG(Duration)/1000)

It's sometimes easier just to slap on a COMPUTE than to rejigger the query ... and, as so often, in this case it avoids another full scan of the table (COMPUTE's a fun cheat, it died too young!).

Note that the COMPUTEd value(s) are a separate result set, and do not have any column heading other than the function used to create them, i.e., column is named "sum" or "avg" or "count" or whatever.


SQL DBA,SQL Server MVP('07, '08, '09)

Carl Sagan said: "There is no such thing as a dumb question." Sagan obviously never watched a congressional hearing!
Post #1436969
Posted Friday, March 29, 2013 9:24 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
ScottPletcher (3/29/2013)
Until you get to SQL 2012, you also have the "quick-and-dirty" version of that, using COMPUTE:

SELECT Curr_date, Action, AVG(Duration)/1000 as Avg_Duration
FROM Table_1
WHERE Curr_date = CAST(GETDATE() as DATE)
GROUP BY Curr_date, Action
-- added AVG just to show that multiple COMPUTE functions can be specified
COMPUTE SUM(AVG(Duration)/1000), AVG(AVG(Duration)/1000)

It's sometimes easier just to slap on a COMPUTE than to rejigger the query ... and, as so often, in this case it avoids another full scan of the table (COMPUTE's a fun cheat, it died too young!).

Note that the COMPUTEd value(s) are a separate result set, and do not have any column heading other than the function used to create them, i.e., column is named "sum" or "avg" or "count" or whatever.


I never even think about COMPUTE. I have seen it before but never really used it. Thanks for the reminder of that long forgotten and neglected feature.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436977
Posted Friday, March 29, 2013 9:40 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 8:56 AM
Points: 139, Visits: 385
I want to push my luck and add one more wrinkle. Say the Curr_Date column is DATETIME and the output of the MyAverage CTE express is grouped by HourOfDay like this:

Curr_date HourOfDay Action Avg_Duration
2013-03-29 07 SignOn 0.950123
2013-03-29 07 GetBal 0.386373
2013-03-29 08 SignOn 0.891234
2013-03-29 08 GetlBal 0.423432
2013-03-29 09 SignOn 0.912342
2013-03-29 09 GetBal 0.372323

And I need to sum those average durations within each hour. So the output would look like:

Curr_date HourOfDay Action Avg_Duration SummedAverage
2013-03-29 07 SignOn 0.950123 1.336496
2013-03-29 07 GetBal 0.386373 1.336496
2013-03-29 08 SignOn 0.891234 1.314666
2013-03-29 08 GetlBal 0.423432 1.314666
2013-03-29 09 SignOn 0.912342 1.284665
2013-03-29 09 GetBal 0.372323 1.284665

Any tricks you can show me there?
Post #1436985
Posted Friday, March 29, 2013 9:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
Ken Davis (3/29/2013)
I want to push my luck and add one more wrinkle. Say the Curr_Date column is DATETIME and the output of the MyAverage CTE express is grouped by HourOfDay like this:

Curr_date HourOfDay Action Avg_Duration
2013-03-29 07 SignOn 0.950123
2013-03-29 07 GetBal 0.386373
2013-03-29 08 SignOn 0.891234
2013-03-29 08 GetlBal 0.423432
2013-03-29 09 SignOn 0.912342
2013-03-29 09 GetBal 0.372323

And I need to sum those average durations within each hour. So the output would look like:

Curr_date HourOfDay Action Avg_Duration SummedAverage
2013-03-29 07 SignOn 0.950123 1.336496
2013-03-29 07 GetBal 0.386373 1.336496
2013-03-29 08 SignOn 0.891234 1.314666
2013-03-29 08 GetlBal 0.423432 1.314666
2013-03-29 09 SignOn 0.912342 1.284665
2013-03-29 09 GetBal 0.372323 1.284665

Any tricks you can show me there?


Can you post ALL of the requirements? Also it would be helpful if you could post your data in a consumable format. Notice in my first post you don't have to do any extra work to get data because I posted it.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1436989
Posted Friday, March 29, 2013 11:14 AM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, September 17, 2014 8:56 AM
Points: 139, Visits: 385
Understood. I won't add any more requirements.

The table:

CREATE TABLE table_1(
[Curr_date] [datetime] NULL,
[Action] [char](15) NULL,
[Duration] [float] NULL
)


Some data:

INSERT INTO Table_1
(Curr_date,Action,Duration)
SELECT 'Mar 29 2013 7:05AM','SignOn ','1000.12' UNION ALL
SELECT 'Mar 29 2013 7:30AM','GetBal ','450.123' UNION ALL
SELECT 'Mar 29 2013 8:15AM','SignOn ','900.123' UNION ALL
SELECT 'Mar 29 2013 8:32AM','GetBal ','300.123' UNION ALL
SELECT 'Mar 29 2013 8:45AM','SignOn ','1100.12' UNION ALL
SELECT 'Mar 29 2013 9:10AM','GetBal ','475.123' UNION ALL
SELECT 'Mar 29 2013 9:12AM','SignOn ','950.123' UNION ALL
SELECT 'Mar 29 2013 9:38AM','GetBal ','320.123' UNION ALL
SELECT 'Mar 29 2013 9:49AM','SignOn ','800.123'


Here is what I am trying.

;with MyAverage as
(
SELECT CAST(Curr_date as DATE) as CurrentDate, DATEPART(hour, Curr_date) as HourOfDay, Action, AVG(Duration)/1000 as Avg_Duration
FROM Table_1
WHERE CAST(Curr_date as Date) = CAST(GETDATE() as DATE)
GROUP BY DATEPART(hour, Curr_date), Action, Curr_date
)
SELECT CurrentDate, HourOfDay, Action, Avg_Duration, (SELECT sum(AVG_Duration) FROM MyAverage) as SummedAverage
FROM MyAverage


You can see what that returns. My goal is to get 6 rows, 2 for each hour, with the SignOn and GetBal averages added together. So with this data SummedAverage will be about 1.45 for the 7:00 hour.
Thanks for your time.
Post #1437026
Posted Friday, March 29, 2013 12:22 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
Thanks having ddl and data makes this a lot easier to deal with. I think you want something like this.

;with MyNewAverage as
(
select Action, Duration, Curr_date, DATEPART(hour, Curr_date) as HourOfDay, AVG(Duration)/1000 as Avg_Duration
from table_1
group by Action, Duration, Curr_date, DATEPART(hour, Curr_date)
)
SELECT HourOfDay, Action, sum(Avg_Duration), (SELECT sum(AVG_Duration) FROM MyNewAverage where DATEPART(hour, cast(Curr_date as datetime)) = m.HourOfDay) as SummedAverage
FROM MyNewAverage m
group by HourOfDay, Action
order by HourOfDay, Action



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1437049
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse