Here are the exemple tables:
Product Prices (prd)
| start_date | product | price |
|---|---|---|
| 2023-04-01 | prod_A | 10.0 |
| 2023-04-15 | prod_A | 20.0 |
| 2023-04-01 | prod_B | 20.0 |
Order Products (ord)
| order_date | id | product |
|---|---|---|
| 2023-04-01 | 10001 | prod_A |
| 2023-04-01 | 10001 | prod_B |
| 2023-04-02 | 10002 | prod_A |
| 2023-04-02 | 10002 | prod_B |
| 2023-04-16 | 10003 | prod_A |
| 2023-04-16 | 10003 | prod_B |
Desired Result
| order_date | id | product | price |
|---|---|---|---|
| 2023-04-01 | 10001 | prod_A | 10.0 |
| 2023-04-01 | 10001 | prod_B | 20.0 |
| 2023-04-02 | 10002 | prod_A | 10.0 |
| 2023-04-02 | 10002 | prod_B | 20.0 |
| 2023-04-16 | 10003 | prod_A | 20.0 |
| 2023-04-16 | 10003 | prod_B | 20.0 |
My first attempt was the following approach:
SELECT ord.order_date, ord.id, ord.product, prd.price
FROM tra
LEFT JOIN (
SELECT *
FROM prd
ORDER BY prd.start_date ASC
) AS prd ON ord.id = prd.id AND ord.order_date >= prd.start_date
But some records keep getting duplicated, like:
Undesired Result
| order_date | id | product | price |
|---|---|---|---|
| 2023-04-16 | 10003 | prod_A | 10.0 |
| 2023-04-16 | 10003 | prod_B | 20.0 |
I know why they are duplicated but don't know what to do. Any idea how I could fix the query?
This is called a lateral join, and the syntax is different (or may not be supported at all) depending on what kind of database you have. SQL Server uses
APPLY, for example. Additionally, very often you can re-write the query using a windowing function for better performance.