Table 1
|Part| Date | Price |
|A | 1-May-2021 |10 |
|B | 1-June-2021 |20 |
Table 2
|Part| Date | Quantity |
|A | 15-May-2021 |4 |
|B | 25-June-2021 |5 |
Need Output as below
|Part| Date | Quantity | Price | Amount
|A | 15-May-2021 |4 | 10 | 40
|B | 25-June-2021 |5 | 20 | 100
So on the effective date of 15-May-21, the price was 10 for Part A , so it should bring me the effective rate and multiply with Qty
2
Answers
Assuming the Date columns are actual sane date types and not insane/broken varchar types:
Is the
Date
column really using that date format? If so, then here’s a couple of suggestion:JOIN
them by usingSUBSTRING_INDEX()
to get the month & year fromDate
column:JOIN
them by using EXTRACT to extract the year & month from a convertedDate
column. Using STR_TO_DATE, you can convert theDate
to MySQL standard date format provided that the date value is consistent with the DATE_FORMAT specifier:I think that those two options above are effective in most cases. However, I personally would prefer using the proper MySQL standard date datatype instead. In that case, I’d add another date column and use that for the date function:
Demo fiddle