SQL Return 1,0 in new variable based on case when statement referring to multiple other variables

后端 未结 1 1618
南笙
南笙 2021-01-25 08:27

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://sqlfid

相关标签:
1条回答
  • 2021-01-25 09:03
    • Since there is no Primary Key defined, I am assuming that data is entered in sequential order on creation_date and ladder_level. Remember that data is stored in unordered fashion in MySQL.
    • Firstly, we have to use a sub-select query to get the data in required order (as highlighted in previous step). It is noteworthy that Order By is executed after Select clause; so we will need to first get the data sorted, and then use the result-set as a Derived Table.
    • Now, we will take the help of User-defined variables (session-level persistent and accessible). In another derived table user_init_vars, we initialize them.
    • In the 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 the ladder_change value.

    Query #1

    SELECT 
      dt.ID, 
      CASE WHEN DATE(@rd) <> DATE(dt.record_date) AND 
                dt.ladder_level > @ll 
           THEN 1 
           ELSE 0 
      END AS ladder_change, 
      @rd := dt.record_date AS record_date, 
      @ll := dt.ladder_level AS ladder_level 
    FROM (SELECT ID, record_date, ladder_level 
          FROM conv_example 
          ORDER BY ID, record_date, ladder_level) AS dt 
    CROSS JOIN (SELECT @rd := '', 
                       @ll := '') AS user_init_vars;
    
    | ID    | ladder_change | record_date         | ladder_level |
    | ----- | ------------- | ------------------- | ------------ |
    | 324   | 0             | 2016-09-15 00:00:00 | a            |
    | 324   | 0             | 2016-09-15 00:00:00 | b            |
    | 324   | 0             | 2017-04-07 00:00:00 | b            |
    | 324   | 0             | 2017-04-07 00:00:00 | c1           |
    | 324   | 0             | 2018-09-08 00:00:00 | c1           |
    | 324   | 0             | 2018-09-08 00:00:00 | e            |
    | 1234  | 0             | 2013-04-03 00:00:00 |              |
    | 1234  | 0             | 2014-07-03 00:00:00 | a            |
    | 1234  | 1             | 2015-04-01 00:00:00 | b            |
    | 1234  | 1             | 2016-09-15 00:00:00 | d            |
    | 1234  | 0             | 2017-02-04 00:00:00 | b            |
    | 1234  | 0             | 2017-04-03 00:00:00 | b            |
    | 1234  | 1             | 2017-04-07 00:00:00 | c1           |
    | 1234  | 1             | 2018-09-08 00:00:00 | e            |
    | 31431 | 0             | 2013-04-03 00:00:00 |              |
    | 31431 | 0             | 2014-07-03 00:00:00 | a            |
    | 31431 | 1             | 2017-04-07 00:00:00 | c1           |
    | 31431 | 1             | 2018-09-08 00:00:00 | e            |
    

    View on DB Fiddle

    0 讨论(0)
提交回复
热议问题