Unix Technical Forum

simple query question

This is a discussion on simple query question within the pgsql Novice forums, part of the PostgreSQL category; --> Hi..... Imagine I have view named view_stok_table_total.... SELECT * FROM view_stok_table_total; index name sum buying_price sale_price 1 mentos 5 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Novice

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-17-2008, 09:28 PM
Akbar
 
Posts: n/a
Default simple query question

Hi.....

Imagine I have view named view_stok_table_total....
SELECT * FROM view_stok_table_total;
index name sum buying_price sale_price
1 mentos 5 100 120
2 mentos 8 110 140
3 durex 9 200 210
4 queen 10 400 450

You see, there are two mentos ( same product but different
price )......
What query command that I have to issue so that I get the list
like this:
name sum buying_price sale_price
mentos 13 110 140
durex 9 200 210
queen 10 400 450

so this time, there is only one mentos. This mentos has 13 ( 8 + 5 )
stuff, and use the highest index's ( that is 2 because 2 is higher than
1 ) buying_price and sale_price value.

Thank you....

regards,

akbar


---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-17-2008, 09:28 PM
Michael Fuhr
 
Posts: n/a
Default Re: simple query question

On Mon, Feb 07, 2005 at 07:23:20PM +0700, Akbar wrote:
>
> What query command that I have to issue so that I get the list
> like this:
> name sum buying_price sale_price
> mentos 13 110 140
> durex 9 200 210
> queen 10 400 450
>
> so this time, there is only one mentos. This mentos has 13 ( 8 + 5 )
> stuff, and use the highest index's ( that is 2 because 2 is higher than
> 1 ) buying_price and sale_price value.


You can get each name's sum and highest index with an aggregate:

SELECT name, sum(sum), max(index) AS index
FROM view_stok_table_total
GROUP BY name;

name | sum | index
--------+-----+-------
mentos | 13 | 2
queen | 10 | 4
durex | 9 | 3
(3 rows)

You could then join those results with the view to get the price
columns for each index:

SELECT ag.name, ag.sum, v.buying_price, v.sale_price
FROM view_stok_table_total AS v
JOIN (SELECT name, sum(sum), max(index) AS index
FROM view_stok_table_total
GROUP BY name) AS ag USING (index)
ORDER BY index;

name | sum | buying_price | sale_price
--------+-----+--------------+------------
mentos | 13 | 110 | 140
durex | 9 | 200 | 210
queen | 10 | 400 | 450
(3 rows)

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

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faq

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-17-2008, 09:28 PM
John K. Herreshoff
 
Posts: n/a
Default Re: simple query question

On Monday 07 February 2005 07:23 am, Akbar 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;

HTH.

John.

> Hi.....
>
> Imagine I have view named view_stok_table_total....
> SELECT * FROM view_stok_table_total;
> index name sum buying_price sale_price
> 1 mentos 5 100 120
> 2 mentos 8 110 140
> 3 durex 9 200 210
> 4 queen 10 400 450
>
> You see, there are two mentos ( same product but different
> price )......
> What query command that I have to issue so that I get the list
> like this:
> name sum buying_price sale_price
> mentos 13 110 140
> durex 9 200 210
> queen 10 400 450
>
> so this time, there is only one mentos. This mentos has 13 ( 8 + 5 )
> stuff, and use the highest index's ( that is 2 because 2 is higher than
> 1 ) buying_price and sale_price value.
>
> Thank you....
>
> regards,
>
> akbar
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-17-2008, 09:28 PM
Michael Fuhr
 
Posts: n/a
Default Re: simple query question

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/

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:01 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com