I'm trying to create a new variable that populates with a 1 (true), 0 (false) in MySQL based on a series of dates and ladder levels (null-E).
See fiddle: http://sqlfiddle.com/#!9/9975e1
Where the record_dates and ladder_levels aren't necessarily in sequential order. I'd like to return the ladder_change fieldvia a case when (?) statement, that says something like:
- First, look only within matching IDs (i.e. just for ID 324)
- Then, something like:
case when record_date2 > record_date1 AND (ladder_level2 < ladder_level1 OR ladder_level2>ladder_level1) then 1, else 0
- Then, something like:
Any tips on how to achieve this?
creation_date
andladder_level
. Remember that data is stored in unordered fashion in MySQL.Order By
is executed afterSelect
clause; so we will need to first get the data sorted, and then use the result-set as a Derived Table.user_init_vars
, we initialize them.Select
clause, we compare the current row's value against the previous row value. After comparison, we set the variable value to current row's value. You can think of it as looping technique, which we use in other programming languages like PHP, C++, Java etc.Case .. When
expressions are used for comparison, and determining theladder_change
value.Query #1
View on DB Fiddle