Sorry if i don't format everything correctly, it's my first time posting here, and sorry for bad english.
I have a big query that returns something like this :
col1 | col2 |col3|col4
==========================
A |A1 |A2 |01/01/20
A |A1 |A3 |01/02/20
A |' ' |' ' |20/02/20
B |' ' |' ' |01/01/20
C |C1 |C2 |01/01/20
C |C1 |C3 |01/02/20
And i want to insert this result into a table like this :
col1 | col2 |col3|col4
==========================
A |A1 |A3 |01/02/20
B |' ' |' ' |01/01/20
C |C1 |C3 |01/02/20
I tried something like:
MERGE INTO dest_table d
USING (SELECT ....) a
on a.col1 = d.col1
WHEN NOT MATCHED THEN INSERT
WHEN MATCHED THEN IF col2 = ' ' THEN do nothing ELSE compare date and keep the most recent one
When i do this, it insert every rows
How i want it to work is like :
- Insert "A;A1;A2" because NOT MATCHED
- "A;A1;A3" IS MATCHED, because "A" was just inserted previously, so enter the "MATCHED" case
- "A;' ';' '" same as the previous one
Can i do something like that with "MERGE INTO" or should i try another method ?
col1based on the date incol4? If so use something likerownum() over (partition by col1 order by col4 desc) = 1for filter. - Marmite Bomber