I have following table in postgres (product and variant are foreign key ids to another tables but I used product/variant names to make it more readable):
| id | productId | variantId | validFrom | currencyCode | price |
|---|---|---|---|---|---|
| 1 | iphone | X | 2021-01-01 | EUR | 19 |
| 2 | iphone | X | 2021-02-01 | EUR | 29 |
| 3 | iphone | X | 2021-03-01 | EUR | 39 |
| 4 | iphone | 12 | 2021-01-01 | EUR | 49 |
| 5 | macbook | air | 2021-01-01 | EUR | 59 |
| 6 | macbook | air | 2021-02-01 | EUR | 69 |
| 7 | macbook | pro | 2021-02-01 | EUR | 79 |
| 8 | macbook | pro | 2021-01-01 | USD | 89 |
Now when I need pricelist valid for a date 2021-01-20 the result should be:
| product | variant | validFrom | currencyCode | price |
|---|---|---|---|---|
| iphone | X | 2021-01-01 | EUR | 19 |
| iphone | 12 | 2021-01-01 | EUR | 49 |
| macbook | air | 2021-01-01 | EUR | 59 |
| macbook | pro | 2021-01-01 | USD | 89 |
but when I need pricelist valid for a date 2021-02-08 the result should be:
| product | variant | validFrom | currencyCode | price |
|---|---|---|---|---|
| iphone | X | 2021-02-01 | EUR | 29 |
| iphone | 12 | 2021-01-01 | EUR | 49 |
| macbook | air | 2021-02-01 | EUR | 69 |
| macbook | pro | 2021-02-01 | EUR | 79 |
| macbook | pro | 2021-01-01 | USD | 89 |
Any suggestion how to write the query?
Many thanks