Created
March 22, 2015 22:39
-
-
Save Bigcheese/20e0ae4552e992fdaee8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
WITH bob AS ( | |
WITH adena AS ( | |
SELECT | |
prices.guid,prices.type,prices.store,prices.url,prices.datetime,prices.quantity,prices.price,prices.currency | |
FROM prices | |
INNER JOIN ( | |
SELECT | |
guid,store,type,max(datetime) m | |
FROM prices | |
GROUP BY guid,store,type | |
) vew ON (prices.guid=vew.guid AND prices.store=vew.store AND prices.datetime=m AND prices.type=vew.type)) | |
SELECT DISTINCT ON (guid, type) | |
* | |
FROM adena | |
ORDER BY guid, type, price ASC | |
) | |
SELECT | |
price, guid, currency, datetime, stores.name AS store, quantity, type, urls.url AS url | |
FROM bob | |
JOIN urls ON (bob.url = urls.id) | |
JOIN stores ON (bob.store = stores.id) | |
ORDER BY price DESC; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment