I need to fix an SQL statement in MySQL that should calculate a field using values from two of the columns and I prefer to do this using set-based programming, ie not procedural. What needs to happen is that in a separate column called "delta" the value of "level" is copied depending on whether "in_event" is 0 or 1. If it is 1, then the value of level is copied, else the last known value of delta is copied.
This is similar to how data is copied down in spreadsheets for underlying cells that have no value, except that in this case data is copied up instead of down.
I have gotten as far as the following, but it copies the data down instead of up:
alter table events add column delta decimal(10, 2);
update events set delta = level where in_event = 1;
select date, sortorder, level, in_event, case when delta is not NULL && @d := delta then delta else @d end as delta
order by date desc;
Maybe you should simplify this and INSERT the data in the table in a loop versus trying to import all the data in one single INSERT chuck? Sometimes doing things in a simple way (INSERT in loop) have easier results to understand that some sweeping method, all in one statement method (INSERT).