query: last N price for each product? - Mailing list pgsql-sql

From David Garamond
Subject query: last N price for each product?
Date
Msg-id 7c33d060807040535y63d7cd1foeed539876eb7c7f8@mail.gmail.com
Whole thread Raw
Responses Re: query: last N price for each product?
Re: query: last N price for each product?
List pgsql-sql
Dear SQL masters,

The query for "latest price for each product" goes like this (which I
can grasp quite easily):
SELECT * FROM price p1WHERE ctime=(SELECT MAX(ctime) FROM price p2 WHERE p1.product_id=p2.product_id)

or:
SELECT * FROM price p1WHERE 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.

Regards,
dave


pgsql-sql by date:

Previous
From: Peter Eisentraut
Date:
Subject: Re: nesting XmlAgg
Next
From: "Claus Guttesen"
Date:
Subject: Re: query: last N price for each product?