Skip to content
Advertisement

How to add two conditions in merge statement using rowid and rownum

Query to load the sequence for e_uuid 13 & 15

Then I want to merge into the same table for e_uuid 14 & 16 For 14: It should check e_uuid = 13 and maximum seq_cnt. Here(after executing my merge statement) maximum seq_cnt is 2 then the seq_cnt for 14 will come as 3 & 4. And if there are any null values then need to give by default 1 in seq_cnt

For 16: It should check e_uuid = 15 and maximum seq_cnt. Here(after executing my merge statement) maximum seq_cnt is 2 then the seq_cnt for 16 will come as 3 & 4.

Output after executing the merge statement given above

Expected Output:

Advertisement

Answer

If you’re wanting to group an odd and a consecutive even e_uuid together, you could always change the partition by clause to group on ceil(e_uuid/2), like so:

db<>fiddle

This works because 13/2 = 6.5, and the ceiling value of 6.5 = 7. 14/2 = 7, the ceiling value of which is also 7, since it’s already an integer. That makes 13 and 14 grouped together – same logic applies to 15 and 16 – the ceiling of them divided by two comes to 8 for both values.

So that explains the logic behind grouping them together (13 is odd, and 14 is the next consecutive even number).

The rows are then ordered by e_uuid, and then rowid to get the ordering you were after.

If you are only ever concerned about the 13, 14, 15, and 16 e_uuids, you might want to have a filter in your x subquery of where e_uuid in (13, 14, 15, 16), depending on if there are other values of e_uuid in your table.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement