masri999
3/21/2007 6:01:00 PM
On Mar 21, 10:53 pm, "Tyrenta" <dougre...@gmail.com> wrote:
> Hi all -- I've searched but can't seem to figure this out -- trying to
> write a query that returns an average of one column grouped by another
> set of criteria, as in:
>
> Table columns: item_id, rater_id, rating
>
> Output: item_id, rater_id, rating, average rating (all raters, per
> item)
>
> Can I do this with one query rating than two? i.e. combine:
>
> Query 1:
> SELECT [table].item_id, AVG([table].rating) as AvgRating FROM [table]
> GROUP BY [table].item_id
>
> with
>
> Query 2:
> SELECT [table].item_id, [table].rater_id, [table].rating,
> [query1].avgrating from table JOIN query ON table.item_id =
> query.item_id
I think what you are looking for is
SELECT [table].item_id, [table].rater_id, [table].rating,
query.avgrating from table JOIN
(SELECT [table].item_id, AVG([table].rating) as AvgRating FROM [table]
GROUP BY [table].item_id ) as query ON table.item_id =
query.item_id