Subquery question:

admin

Administrator
Staff member
Hey all!

I'm using PostgreSQL 7.1 - question regarding subqueries.

Given a table defined thusly:

sample
=========
category integer not null,
value integer not null,
userid integer not null


Each user has multiple values given in multiple categories and there are many users.

I want an average of all values given in each category that belong to a particular user, as in

"select avg(value) from sample where category=(select distict category from sample where userid=5)"

where the output might be:

cat / value
=============
1 / 3
2 / 2.2
3 / 5.87


The above query doesn't work, I get:

ERROR: More than one tuple returned by a subselect used as an expression.

Any takers?
 
Back
Top