For the following data:
PGM INC EXC
First A AA
First B BB
First C CC
First D DD
Second E EE
Second F FF
Second G GG
Second H HH
How can we sequentially read column data of PGM='First' and update into Columns of PGM='Second'
PGM INC EXC
Second E AA
Second F BB
Second G CC
Second H DD
Here as we can see PGM='Second' has mapped the same data of PGM='First' for the 4 rows. How can this be achieved in a single update query of such multiple records.
Please guide.
The basic idea is to use the row_number window function to assign 1, 2, 3 etc in order to the two sets. You can then join these to figure out which value from set 1 should apply to which value in set 2.
Here's a lowest common denominator method that works on Postgres/Oracle/SQL Server. I don't have access to DB2 to try it.
if there aren't enough values in First, then the end values for second will be overwritten with null
Update
test
Set
Exc = (
Select
f.Exc
From (
Select
Exc,
row_number() over (order by inc) as rn
From
test
Where
Pgm = 'First'
) as f
inner join (
Select
Inc,
row_number() over (order by inc) as rn
From
test
Where
Pgm = 'Second'
) as s
On f.rn = s.rn
Where
test.inc = s.inc
)
Where
test.Pgm = 'Second';
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.