Sunday, February 19, 2012
AVG by columns not by rows
rows.
E.g.
col1 col2 avg
--
2 2 2.0
3 2 2.5
I found something that requires manual sum of every column and than
dividing with number of nonzero an nonnull columns. I ended using tons
of case when... statements.
Is there any simple solution?Assuming you have a PK on your table
SELECT PK, AVG(AvgCol)
FROM
(SELECT PK, C1 AS AvgCol
FROM YourTable
UNION
SELECT PK, C2
FROM YourTable) T
GROUP BY PK
Regards
Roji. P. Thomas
http://toponewithties.blogspot.com
<milan.letic@.gmail.com> wrote in message
news:1151930480.771154.61470@.v61g2000cwv.googlegroups.com...
> Hello is there a way to calculate Average values for columns not for
> rows.
> E.g.
> col1 col2 avg
> --
> 2 2 2.0
> 3 2 2.5
> I found something that requires manual sum of every column and than
> dividing with number of nonzero an nonnull columns. I ended using tons
> of case when... statements.
> Is there any simple solution?
>|||If you need the entire original row, as I suspect you will, you will
need to join this back to the orignal table:
SELECT T.KeyCol,
AvgX = AVG(CASE WHEN N.X = 1 AND T.col1 <> 0 THEN T.col1
WHEN N.X = 2 AND T.col2 <> 0 THEN T.col2
WHEN N.X = 3 AND T.col3 <> 0 THEN T.col3
WHEN N.X = 4 AND T.col4 <> 0 THEN T.col4
WHEN N.X = 5 AND T.col5 <> 0 THEN T.col5
END)
FROM TheTable as T,
(select 1 as X UNION ALL
select 2 UNION ALL
select 3 UNION ALL
select 4 UNION ALL
select 5) as N
GROUP BY T.KeyCol
Also, your sample data appears to start with integer values but the
average is to one decimal place, so it appears some conversion before
the AVG is applied will be required.
Roy Harvey
Beacon Falls, CT
On 3 Jul 2006 05:41:20 -0700, milan.letic@.gmail.com wrote:
>Hello is there a way to calculate Average values for columns not for
>rows.
>E.g.
>col1 col2 avg
>--
>2 2 2.0
>3 2 2.5
>I found something that requires manual sum of every column and than
>dividing with number of nonzero an nonnull columns. I ended using tons
>of case when... statements.
>Is there any simple solution?|||If your table has a primary key, you can unpivot the result and group by the
primary key and then join the result to the original table.
create table dbo.t1 (
pk int not null identity primary key,
c1 int,
c2 int
)
go
insert into dbo.t1(c1, c2) values(2, 2)
insert into dbo.t1(c1, c2) values(3, 2)
go
select
t1.pk,
t1.c1,
t1.c2,
t2.col_avg
from
dbo.t1
inner join
(
select
a.pk,
avg(cast(
case b.c1
when 1 then a.c1
when 2 then a.c2
end as numeric(5, 2))) as col_avg
from
dbo.t1 as a
cross join
(select 1 as c1 union all select 2) as b
group by
a.pk
) as t2
on t1.pk = t2.pk
go
drop table dbo.t1
go
AMB
"milan.letic@.gmail.com" wrote:
> Hello is there a way to calculate Average values for columns not for
> rows.
> E.g.
> col1 col2 avg
> --
> 2 2 2.0
> 3 2 2.5
> I found something that requires manual sum of every column and than
> dividing with number of nonzero an nonnull columns. I ended using tons
> of case when... statements.
> Is there any simple solution?
>|||Guys, thank you. But, I don't understand your code. I don't know what
it does, which is more efficient.. I'm afraid I'll have to use my old
version.
Alejandro Mesa wrote:
> If your table has a primary key, you can unpivot the result and group by t
he
> primary key and then join the result to the original table.
> create table dbo.t1 (
> pk int not null identity primary key,
> c1 int,
> c2 int
> )
> go
> insert into dbo.t1(c1, c2) values(2, 2)
> insert into dbo.t1(c1, c2) values(3, 2)
> go
> select
> t1.pk,
> t1.c1,
> t1.c2,
> t2.col_avg
> from
> dbo.t1
> inner join
> (
> select
> a.pk,
> avg(cast(
> case b.c1
> when 1 then a.c1
> when 2 then a.c2
> end as numeric(5, 2))) as col_avg
> from
> dbo.t1 as a
> cross join
> (select 1 as c1 union all select 2) as b
> group by
> a.pk
> ) as t2
> on t1.pk = t2.pk
> go
> drop table dbo.t1
> go
>
> AMB
> "milan.letic@.gmail.com" wrote:
>|||Hi There,
Roji has already given you a solution , I had changed it minorly by
using UNION ALL instead of UNION and added where clause although it is
not required as much.
Here R is a value that uniquely identifies a row (i.e Primary key )
Create view tmpData3
as
Select 1 R,null a,2 b, 3 c
Union
Select 2 R,1 a,null b, 3 c
Union
Select 3 R,1 a,2 b, null c
Union
Select 4 R,1 a,null b, null c
Go
Select R,avg(col) From
(
Select R,cast(a as numeric) col from tmpData3 where a>0
Union All
Select R,b from tmpData3 where b>0
Union All
Select R,c from tmpData3 where c>0
)X group by R
drop view tmpData3
With Warm regards
Jatinder Singh
http://jatindersingh.blogspot.com
milan.letic@.gmail.com wrote:
> Guys, thank you. But, I don't understand your code. I don't know what
> it does, which is more efficient.. I'm afraid I'll have to use my old
> version.
>
> Alejandro Mesa wrote:
Thursday, February 16, 2012
averageofChildren vs avg()/count()
hi,
which is better to use: averageofchildren or avg()/count() calculated measure, if i don't want to calculate average over time.
i have read in the "Analysis Services Performance Guide" that is better to avoid using semi-additive functions to improve performance.
Special aggregate functions require unique performance-tuning techniques. They
include DistinctCount and a collection of semiadditive aggregate functions. The
semiadditive functions include: FirstChild, LastChild, FirstNonEmpty,
LastNonEmpty, ByAccount, and AverageOfChildren.
thanks
Christina
Probably the normal average (sum/count) is the better choice if you won't consider time. AverageOfChildren requires a time dimension and Enterprise Edition.|||i have both time dimension and enterprise edition.
but my question is, i have lots of measures in which i will be using the average. i would like to know if i use averageofchildren is convenient or better use the other way, in terms of performance.
thanks
|||Now I am not answering your question again, but.. since the average of sum/count will be a calculated member, it will take zero processing time for the calculation, but the sum and count will take processing time (both probably much faster than AverageOfChildren). However, it will take time to calculate the value when a user executes the query. AvaregeOfChildren is used for measures and therefore will take some time to process, but then zero extra time for queries (if aggregated).
How they compare (process sum+count + calculate average vs just process) I don't know, but you can probably test this on your specific system. In general using measures is better than calculated members (faster for end users, but as said, that depends on how long it takes to process the measure vs calculate the member).
Cheers
Thanks HappyCow.
i think i got the point. I'll go for averageofChildren and test accordingly. if i find it slow, will try the other way.
Hope you remain always happy..but not a cow.
Cheers
Average Times
following expression to calculate the time
=IIF(RowNumber("DailyInspectCount") > 1,-1 *
(DateDiff(DateInterval.Minute,Fields!InspectedByDate.Value,Previous(Fields!InspectedByDate.Value))),0)
& " min."
This gives me the number of minutes between each event just fine. Now I
want to get an overall average for the whole row. When I use the AVG
function I get the error "Aggregate functions cannot be nested inside other
aggregate functions". Does anyone know a way to get the values from this
expression to generate the average? Thanks.
JohnCorrection,
I want to get the average for the whole column not the row.
"John Wright" <riley_wright@.hotmail.com> wrote in message
news:uVA4BwvpIHA.4716@.TK2MSFTNGP06.phx.gbl...
>I have a report that displays the average time between events. I use the
>following expression to calculate the time
> =IIF(RowNumber("DailyInspectCount") > 1,-1 *
> (DateDiff(DateInterval.Minute,Fields!InspectedByDate.Value,Previous(Fields!InspectedByDate.Value))),0)
> & " min."
>
> This gives me the number of minutes between each event just fine. Now I
> want to get an overall average for the whole row. When I use the AVG
> function I get the error "Aggregate functions cannot be nested inside
> other aggregate functions". Does anyone know a way to get the values from
> this expression to generate the average? Thanks.
>
> John
>
Average of Average
Hi guys,
i need to calculate the average of the average in a hierarchy
my hierarchy looks like this
1
1.1 Average: 3
1.1.1 Value: 1
1.1.2 Value: 5
1.1.3 Value: 1
1.1.4 Value: 5
1.1.5 Value: 1
1.1.6 Value: 5
1.2 Average: 5
1.2.1 Value: 5
1.2.2 Value: 5
now i would like to calculate the average of node 1 as avg(1.1, 1.2) what would be 4
on the 2nd level in can simply use
Avg([Item].[Item].currentmember.children, [Measures].[Value])
but how to calculate the average of that average?
the hierarchy i use is an attribute hierarchy with 5 levels and i need the calculation on each level
further i would need this caluclation for all my dimensions/hierarchies...
but i guess that wont be possible without far too much effort
any ideas how i could do this?
Here's an example of a recursive approach in Adventure Works, applied to the [Product Categories] hierarchy:
Code Snippet
With
Member [Measures].[AvgStockLevel] as
iif(IsLeaf([Product].[Product Categories].CurrentMember),
[Product].[Safety Stock Level].MemberValue,
Avg([Product].[Product Categories].Children,
[Measures].[AvgStockLevel])),
FORMAT_STRING = '#.0'
select
{[Measures].[AvgStockLevel]} on 0,
[Product].[Product Categories].Members on 1
from [Adventure Works]
|||
nice, thx
works perfectly!!
i just changed Avg([Product].[Product Categories].Children,
to Avg([Product].[Product Categories].CURRENTMEMBER.Children
so that it is easier to read (for me) - the result is the same
Code Snippet
With
Member [Measures].[AvgStockLevel] as
iif(IsLeaf([Product].[Product Categories].CurrentMember),
[Product].[Safety Stock Level].MemberValue,
Avg([Product].[Product Categories].CURRENTMEMBER.Children,
[Measures].[AvgStockLevel])),
FORMAT_STRING = '#.0'
select
{[Measures].[AvgStockLevel]} on 0,
[Product].[Product Categories].Members on 1
from [Adventure Works]
i didn't even know that it is possible to use recursive calculations within mdx
thx
Gerhard
Average Expression ?
I want to calculate the Average Sales for given columns, eg
Jan Fab Mar April AVERAGE
100 100 100 100 100
20 20 10 15.66
30 10 50 30
Is it possible using an Expression ?
ThanksCreate a matrix report and change the aggregate from Sum to avg
--
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC
I support the Professional Association for SQL Server ( PASS) and it''s
community of SQL Professionals.
"Vishal" wrote:
> Hi,
> I want to calculate the Average Sales for given columns, eg
> Jan Fab Mar April AVERAGE
> 100 100 100 100 100
> 20 20 10 15.66
> 30 10 50 30
> Is it possible using an Expression ?
> Thanks
>
>