Re: simple query question - Mailing list pgsql-novice

From Michael Fuhr
Subject Re: simple query question
Date
Msg-id 20050207162632.GA34287@winnie.fuhr.org
Whole thread Raw
In response to Re: simple query question  ("John K. Herreshoff" <jkherr@power-net.net>)
List pgsql-novice
On Mon, Feb 07, 2005 at 06:17:47AM -0500, John K. Herreshoff wrote:
>
> Try SELECT name, sum(sum) as total_sum, buying_price, sale_price
> FROM view_stok_table_total
> GROUP BY name, buying_price, sale_price
> ORDER BY buying_price, sale_price;

To calculate the total sum for each name, you need to use "GROUP BY name",
not "GROUP BY name, buying_price, sale_price".  For the sample data the
above query gives the following result, which is not what was requested:

  name  | total_sum | buying_price | sale_price
--------+-----------+--------------+------------
 mentos |         5 |          100 |        120
 mentos |         8 |          110 |        140
 durex  |         9 |          200 |        210
 queen  |        10 |          400 |        450
(4 rows)

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: Percent of update completed
Next
From: Colin McGuigan
Date:
Subject: Re: Percent of update completed