Forum Moderators: open

Message Too Old, No Replies

Selecting AVG value and the values in same query?

Is it possible?

         

smagdy

1:00 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



Hello,

Ive this table with 2 rows as an example:
id - name - time - mon - price
4 - n1 - 8 - 350 - 10000
4 - n2 - 5 - 450 - 20000

when i try to query this:

select AVG(time) as time, AVG(mon) as mon, AVG(price) as price from table where id=4

it outputs:
time - mon - price
6.5 - 400 - 15000

and if i edit the query to add "name" which will require adding GROUP BY

select name, AVG(time) as time, AVG(mon) as mon, AVG(price) as price from table where id=4 group by datetime

it outputs:

name - time - mon - price
n1 - 8 - 350 - 10000
n2 - 5 - 450 - 20000

so is there a way to optimize the query and get the average and rest of values in same query result?

or must be 2 queries?

Thanks in advance

FalseDawn

4:47 pm on Aug 21, 2006 (gmt 0)

10+ Year Member



You could use a UNION to retrieve aggregated and non-aggregated data in the same query results, but it will probably be little faster and more cumbersome than just running 2 queries.