Database I'm using is Oracle Database 19C Enterprise edition I am struggling to find a solution to an issue where I need to insert entries into a my main table based on condition from the staging table.
There are 3 scenarios.
Staging table sample 1:
| pool_name | account | flag |
|---|---|---|
| pool-b | acc1 | S |
| pool-b | acc1 | I |
| pool-n | acc2 | S |
| pool-n | acc2 | N |
In the above scenario I need to read this table and insert to a main table only 3 entries, the condition is if pool_name ends with -b exist when the flag is 'S' this entry need to insert and the entry with pool_name which ends with -n and flag is 'S' needs to ignore. Where the flag is 'I' and 'N' always needs to be inserted.
Scenario 2
| pool_name | account | flag |
|---|---|---|
| pool-b | acc1 | I |
| pool-n | acc2 | S |
| pool-n | acc2 | N |
When pool_name ends with -b and flag 'S' does not exists in staging table, pool_name ends with -n and flag 'S' needs to be entered. The tricky part is , if in the main table pool_name which ends with -b and flag 'S' exist then the insert of the pool-n with flag 'S' should not be performed.
Scenario 2
| pool_name | account | flag |
|---|---|---|
| pool-b | acc1 | I |
| pool-b | acc2 | S |
| pool-n | acc2 | N |
When pool_name ends with -n and flag 'S' does not exists in staging table, pool_name ends with -b and flag 'S' needs to be entered. The tricky part here is , if in the main table pool_name which ends with -n and flag 'S' exist then the in the main table this record should be override by the pool-b with hte flag 'S'
I understand this is bit of abstract representation. But if anyone can help me on this that will be greatly appreciated.
I have tried to split the -b and -s and populate in different columns in staging table and try to use that to identify those two entries by using group by, those this approach provided me with a single entry it does not support the logic I mentioned above.
I think you can use: