
The requirement was to add a column called position
to the order_details
table.
The order_details
table is actually a table with a foreign key of order_id
. The requirement is to increment the position starting from 1
in the position
column grouped on order_id
.
So, before the migration if the records are –
id order_id 1 1 2 1 3 1 4 2 5 2 6 2
After the migration, the records should be –
id order_id position 1 1 1 2 1 2 3 1 3 4 2 1 5 2 2 6 2 3
There are two solutions possible. One using window functions
and one without.