Skip to content

Instantly share code, notes, and snippets.

@scalaview
Last active June 30, 2016 03:56
Show Gist options
  • Save scalaview/003f0bbfe5b6ad34b218d2ae6702f3ff to your computer and use it in GitHub Desktop.
Save scalaview/003f0bbfe5b6ad34b218d2ae6702f3ff to your computer and use it in GitHub Desktop.
SELECT history.*, history1.id AS h_id, (history1.retail_price - history.retail_price) AS t_price FROM product_price_histories AS history
LEFT JOIN product_price_histories AS history1 ON history1.product_id = history.product_id
LEFT JOIN products AS sku ON sku.id = history.product_id
LEFT JOIN standard_products AS spu ON spu.id = sku.standard_product_id
WHERE NOT EXISTS (
SELECT t_h.id, t_h.created_at FROM product_price_histories AS t_h
WHERE t_h.id != history.id
AND t_h.product_id = history.product_id
AND t_h.created_at >= '2016-03-29 08:28:54'
AND t_h.created_at > history.created_at
)
AND (history.created_at >= '2016-03-29 08:28:54' AND history1.created_at >= '2016-03-29 08:28:54')
AND (history.retail_price < history1.retail_price) AND (history.created_at > history1.created_at)
GROUP BY history.product_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment