Skip to content

Instantly share code, notes, and snippets.

@Bigcheese
Created March 22, 2015 22:39
Show Gist options
  • Save Bigcheese/20e0ae4552e992fdaee8 to your computer and use it in GitHub Desktop.
Save Bigcheese/20e0ae4552e992fdaee8 to your computer and use it in GitHub Desktop.
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