The priceQuote table, give the category and the price at a date range, however the valid to date is always a very large date.
The other table is Item table, which gives the itemID and its category with a date
The idea is to join these two tables based on its category and the date, for example, ITEMID 0503848220, category 1117, date 2020-07-02 should return $730(amount field).
Below is my attempt, of course it does not work as I wanted it to work
SELECT
Item.ItemID
,Item.Date
,ValidFROM
,ValidTo
,priceQuote.AMOUNT
From
Item
JOIN
priceQuote
on
Item.category=priceQuote.Category
and
Item.Date between ValidFROM and ValidTo
I wonder what is the best way of doing this (considering these two tables are very large and it may have performance issues joining with date range). If there is no good way of doing the join in SQL, I am open to other solutions such as loading these two tables into C# application and do the calculation through C#.
Thanks for your help.
Example of the data( not sure how to attach Excel files here):
+----------+-----------+------------+--------+
| Category | ValidFROM | ValidTo | AMOUNT |
+----------+-----------+------------+--------+
| 1113 | 4/1/2020 | 12/31/9999 | 665 |
| 1113 | 7/1/2020 | 12/31/9999 | 925 |
| 1113 | 5/10/2021 | 12/31/9999 | 491 |
| 1117 | 4/1/2020 | 12/31/9999 | 495 |
| 1117 | 7/1/2020 | 12/31/9999 | 730 |
| 1117 | 1/1/2021 | 12/31/9999 | 556 |
| 1117 | 5/10/2021 | 12/31/9999 | 555 |
+----------+-----------+------------+--------+
Item Table:
+-----------+----------+-----------+
| ItemID | category | Date |
+-----------+----------+-----------+
| 503848220 | 1117 | 7/2/2020 |
| 503848221 | 1117 | 7/2/2020 |
| 503848225 | 1117 | 7/3/2020 |
| 503848227 | 1117 | 7/6/2020 |
| 503848228 | 1117 | 7/6/2020 |
| 503848266 | 1113 | 6/26/2020 |
+-----------+----------+-----------+