Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Average value total dynamic ignoring zeros

Author  Topic 

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2013-11-25 : 09:54:03
Hi

I have this data returning from a query:




Procedure Intermediate New Old Total avg
Proc1 6 0 0 6 2.000000
Proc2 74 13 0 87 29.000000
Proc3 29 0 0 29 9.666666
Proc4 16 0 0 16 5.333333


And I want to dynamically calculate the average rather than divide the total by the number of columns every time, I want to divide only by the number of non zero values per row for intermediate, new and old. So row 1 average would just be itself = 6, row 2 would be 74+13+87/3 etc.


;with cte as
(
select
f.Procedure,
SUM(case when f.old_new = 'O' then f.Value else 0 end) "Intermediate",
SUM(case when f.old_new = 'Y' then f.Value else 0 end) "New",
SUM(case when f.old_new = 'N' then f.Value else 0 end) "Old"
from MyTable f
group by f.Procedure
)
select cte.Procedure,
cte.[Old],
cte.[Intermediate],
cte.[New],
coalesce(cte.[Intermediate], 0) + coalesce(cte.[Old], 0) + coalesce(cte.[New], 0) Total,
avg(Value) [avg]
from cte
cross apply
(
select cast(Intermediate] as decimal(10, 2)) union all
select cast([New] as decimal(10, 2)) union all
select cast([Old] as decimal(10, 2))
) c(Value)
group by Procedure, [Intermediate], [New], [Old]


I got this code at another site but wanted to see what the you SQL guys think I need to do here?

Appreciate the help

G

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-25 : 10:34:20
>>I have this data returning from a query
If you post that query we may be able to modify it to return the results you want to end up with.

>>I got this code at another site but wanted to see what the you SQL guys think I need to do here?
Does it work for you?
Looks to me like they missed using a: "cast([NULLIF,[Intermediate],0) as decimal(10, 2)) union all" around the 3 columns in the cross apply statement.

There's other ways you could do this but unless you are getting bad results or bad performance why bother changing it?

Be One with the Optimizer
TG
Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2013-11-25 : 11:05:42
quote:
Originally posted by TG

>>I have this data returning from a query
If you post that query we may be able to modify it to return the results you want to end up with.

>>I got this code at another site but wanted to see what the you SQL guys think I need to do here?
Does it work for you?
Looks to me like they missed using a: "cast([NULLIF,[Intermediate],0) as decimal(10, 2)) union all" around the 3 columns in the cross apply statement.

There's other ways you could do this but unless you are getting bad results or bad performance why bother changing it?

Be One with the Optimizer
TG



It works to an extent but calculating the average dynamically is something I want to be able to do. In this case the average is only for the columns that have a value not for the count of columns. so if only one column had a value that average would be the same as the value.

Would nullif make any difference to the averages, would this only calculate the true values not include 0
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-11-25 : 11:12:02
>>Would nullif make any difference to the averages, would this only calculate the true values not include 0
That is why I added it.
Sql server will exclude nulls from aggregates but not zeros.

so:
avg(2,2,0,0) = 1
vs.
avg(2,2,null,null) = 2


EDIT:

if NULLs are included in the aggregate you will likely see this message:
Warning: Null value is eliminated by an aggregate or other SET operation.
If that message bother you you can suppress it with:
set ansi_warnings off
That SET will only affect the current user session.

Be One with the Optimizer
TG
Go to Top of Page

Grifter
Constraint Violating Yak Guru

274 Posts

Posted - 2013-11-25 : 11:49:59
Ok I tried that nullif and it worked fine, appreciate the help

Thanks

G

quote:
Originally posted by TG

>>Would nullif make any difference to the averages, would this only calculate the true values not include 0
That is why I added it.
Sql server will exclude nulls from aggregates but not zeros.

so:
avg(2,2,0,0) = 1
vs.
avg(2,2,null,null) = 2


EDIT:

if NULLs are included in the aggregate you will likely see this message:
Warning: Null value is eliminated by an aggregate or other SET operation.
If that message bother you you can suppress it with:
set ansi_warnings off
That SET will only affect the current user session.

Be One with the Optimizer
TG

Go to Top of Page
   

- Advertisement -