Trying to select more than 1 column using aggregate function

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
impulse()
Forum Regular
Posts: 748
Joined: Wed Aug 09, 2006 8:36 am
Location: Staffordshire, UK
Contact:

Trying to select more than 1 column using aggregate function

Post by impulse() »

I'm trying to run the following query, which runs fine, but I also want to select another file. When I try to it says there's an error because it's not part of an aggregate function. Is there a way to select that other column I want?

Code: Select all

SELECT description, SUM(val) as val, ops.product
FROM scheme.opdetm as opd, scheme.opservm ops
WHERE ops.product = opd.product
GROUP BY ops.product
ORDER BY val DESC
Regards,
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Under the assumption that description is the same for all pairs of ops.product you can:

GROUP BY ops.product, description
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Trying to select more than 1 column using aggregate func

Post by califdon »

impulse() wrote:I'm trying to run the following query, which runs fine, but I also want to select another file. When I try to it says there's an error because it's not part of an aggregate function. Is there a way to select that other column I want?

Code: Select all

SELECT description, SUM(val) as val, ops.product
FROM scheme.opdetm as opd, scheme.opservm ops
WHERE ops.product = opd.product
GROUP BY ops.product
ORDER BY val DESC
Not quite sure I understand what you want to do. Do you want to join three tables? That's just a matter of expanding your FROM clause. If that's so, is there a 'val' column in all 3 tables? And do you want the sum total from the 'val' column in all 3?
Post Reply