Howdy,
I need to write an update query with multiple aggregate functions.
Here is an example:
UPDATE t
SET
t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ),
t.b = ( select sum(f.q) from dbo.foo f where f.p = t.y )
FROM dbo.test t
Basically I need to get some aggregate statistics about the rows of
foo and store them in rows of t. The above statement works fine...but
note how the two subSelect's have the exact same WHERE clause. This
screams at me to combine them...but how? I would like to have
something like this in my query:
SELECT avg(f.q), sum(f.q) FROM dbo.foo f WHERE f.p = 2
...and somehow store the results in t.a and t.b. Is there any way to
do this?
Thanks before hand!
I need to write an update query with multiple aggregate functions.
Here is an example:
UPDATE t
SET
t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ),
t.b = ( select sum(f.q) from dbo.foo f where f.p = t.y )
FROM dbo.test t
Basically I need to get some aggregate statistics about the rows of
foo and store them in rows of t. The above statement works fine...but
note how the two subSelect's have the exact same WHERE clause. This
screams at me to combine them...but how? I would like to have
something like this in my query:
SELECT avg(f.q), sum(f.q) FROM dbo.foo f WHERE f.p = 2
...and somehow store the results in t.a and t.b. Is there any way to
do this?
Thanks before hand!
Comment