itspb

Untitled

Jun 21st, 2023
1,585
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 0.42 KB | None | 0 0
  1. WITH items_log AS (
  2.     SELECT
  3.         item_id,
  4.         item_name,
  5.         price,
  6.         update_date AS start_date,
  7.         COALESCE((lead(update_date) OVER(partition BY item_id ORDER BY update_date)) - INTERVAL '1 day', '9999-12-31') AS end_date 
  8.     FROM items
  9. )
  10. SELECT
  11.     o.order_id,
  12.     il.item_name
  13. FROM orders o
  14. INNER JOIN items_log AS il ON (il.item_id = o.item_id AND o.order_date BETWEEN il.start_date AND il.end_date)
  15. WHERE
  16.     il.price > 3
Advertisement
Add Comment
Please, Sign In to add comment