Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Sql > Re: query: last...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 3 Topic 3558 of 3799
Post > Topic >>

Re: query: last N price for each product?

by fbax@[EMAIL PROTECTED] (Frank Bax) Jul 4, 2008 at 08:55 AM

David Garamond wrote:
> Dear SQL masters,
> 
> The query for "latest price for each product" goes like this (which I
> can grasp quite easily):
> 
>  SELECT * FROM price p1
>  WHERE ctime=(SELECT MAX(ctime) FROM price p2 WHERE
p1.product_id=p2.product_id)
> 
> or:
> 
>  SELECT * FROM price p1
>  WHERE NOT EXISTS (SELECT * FROM price p2 WHERE
> p1.product_id=p2.product_id AND p1.ctime < p2.ctime)
> 
> but how do yo query for "latest 3 prices for each product"? The
> "price" table contains current price as well as price histories.


Is this what you are looking for...

SELECT * FROM price p1
WHERE ctime in (SELECT ctime FROM price p2 WHERE 
p1.product_id=p2.product_id ORDER BY ctime DESC LIMIT 3)


-- 
Sent via pgsql-sql mailing list (pgsql-sql@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-sql
 




 3 Posts in Topic:
query: last N price for each product?
davidgaramond@[EMAIL PROT  2008-07-04 19:35:57 
Re: query: last N price for each product?
kometen@[EMAIL PROTECTED]  2008-07-04 14:44:22 
Re: query: last N price for each product?
fbax@[EMAIL PROTECTED] (  2008-07-04 08:55:58 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Dec 2 21:32:46 CST 2008.