mysql - copy column data to next line on when condition in sql -


attendance id         status   activity 1                     0        xyz 2                     1        abc 3                     2        abc 4                     1 

i have column in id unique value ex when status = 2 update next attendance id , copy activity of status = 2 next attendance id i.e. 4

how can that?

if mean want copy status next datarow. can use statement. i've added demo code it.

create table #temp(attendance_id int identity(1,1), status int, acitivity nvarchar(10))  insert #temp(status, acitivity) values(0, n'xyz'),(1,n'abc'),(2,n'abc'),(3,null)  -- see happens select t.attendance_id, t.status oldstatus, isnull(shifting.status,t.status),t.acitivity #temp t left join (     select status, row_number() on (order attendance_id) +1 rn      #temp     ) shifting     on t.attendance_id = shifting.rn  -- update update t set status = isnull(shifting.status,t.status) #temp t left join (     select status, row_number() on (order attendance_id) +1 rn      #temp     ) shifting     on t.attendance_id = shifting.rn  select * #temp  drop table #temp 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

c# - Exception when attempting to modify Dictionary -