Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WITH items_log AS (
- SELECT
- item_id,
- item_name,
- price,
- update_date AS start_date,
- COALESCE((lead(update_date) OVER(partition BY item_id ORDER BY update_date)) - INTERVAL '1 day', '9999-12-31') AS end_date
- FROM items
- )
- SELECT
- o.order_id,
- il.item_name
- FROM orders o
- 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)
- WHERE
- il.price > 3
Advertisement
Add Comment
Please, Sign In to add comment