I have two tables I'm trying to join on date and account. For table one the field for date is continuous and is type string. Whereas for table two the field date has a portion of the date field as end of month only.
Edit: The sample I provided below is an example of tables I am working with. Originally I had only mentioned about joining the ID and Date fields but there are other fields in both tables that I am trying to keep in the final output. On a larger scale Table 1 has thousands of IDs with records continuously recorded daily across multiple years. In Table 2 this is similar but at some point the date field switched from only end of month data to the same daily date data as table one. The tables below have been updated.
A sample of the data set could be seen as follows:
Table1:
| ID | Date |
| -- | ---------- |
| 1 | "2022-01-30" |
| 1 | "2022-01-31" |
| 1 | "2022-02-01" |
| 1 | "2022-02-02"|
Table2:
| ID | Date |Field_flag|
| ---- | ---------- | ------- |
| 1 | "2021-12-31" |a |
| 1 | "2022-01-31" |a |
| 1 | "2022-02-01" |a |
| 1 | "2022-02-02" |b |
| 1 | "2022-02-03"|b |
Desired result:
| table1.ID | table1.Date | table2.date| table2.Field_flag|
| -------- | ---------- | ---------- | --------------- |
| 1 | "2022-01-30"|"2022-01-31"| a |
| 1 | "2022-01-31"|"2022-01-31"| a |
| 1 | "2022-02-01"|"2022-02-01"| a |
| 1 | "2022-02-02"|"2022-02-02"| b |
Are there any suggestions on how to approach this type of result? I'm currently temp fielding and sub sampling the date fields into month and year as a temporary solution but would like something like an inner join as such to work.
Select table1.*
,table2.date as date_table2
,table2.Field_flag
FROM table1
INNER JOIN (SELECT * FROM Table2)
ON table1.id = table2.id and (table1.date = table2.date or table1.date < table2.date)
