Re: [SQL] Query Question - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] Query Question
Date
Msg-id 25093.929545936@sss.pgh.pa.us
Whole thread Raw
In response to Query Question  (Bob Kruger <bkruger@mindspring.com>)
List pgsql-sql
Bob Kruger <bkruger@mindspring.com> writes:
> I would like to be able to do a query in which I can list all of the
> vehicles and a totalization of the costs, e.g. list one vehicle number, and
> the sum of costs for that vehicle.

I think you want GROUP BY, eg
select veh_no, sum(cost) from table group by veh_no;

sum() and other aggregates apply across a group, not across the whole
table, when you use GROUP BY.  Notice you do NOT use DISTINCT ...
GROUP BY takes care of that for you.
        regards, tom lane


pgsql-sql by date:

Previous
From: Bob Kruger
Date:
Subject: Query Question
Next
From: Herouth Maoz
Date:
Subject: Re: [SQL] Query Question