plates
| plate_id | date | product_id |
|---|---|---|
| 1 | 01-12-2020 | 101 |
| 2 | 01-12-2020 | 202 |
| 3 | 02-12-2020 | 101 |
| 4 | 02-12-2020 | 202 |
| 5 | 02-12-2020 | 303 |
production
| order_id | date | product_id | cost |
|---|---|---|---|
| 1001 | 01-12-2020 | 101 | 10.95 |
| 1002 | 01-12-2020 | 202 | 19.00 |
| 1003 | 02-12-2020 | 101 | 11.50 |
| 1004 | 02-12-2020 | 202 | 20.05 |
| 1005 | 02-12-2020 | 303 | 17.00 |
the result of the query I need to get is:
| plate_id | cost |
|---|---|
| 4 | 20.05 |
Their are not related with pk and fk,
each product can have more then one production cost.
I suppose that I need to create some subquery where to take into account always two columns: date and product_id
below query unfortunatelly gives an error
SELECT pl.plate_id, pr.cost
FROM plates pl
JOIN (select * from production pr where pr.date=pl.date and pr.product_id=pl.product_id)
WHERE pl.product_id = 4;
20.05instead of19.00? Are you ordering by latest date or highest cost?