i use sql
i have table row every user every day
uid date attribute 123 2017-01-01 awesome 123 2017-01-02 awesome 123 2017-01-03 awesome 123 2017-01-04 not awesome 123 2017-01-05 awesome 123 2017-01-06 awesome 123 2017-01-07 awesome 166 2017-01-01 kinda awesome 166 2017-01-02 awesome 166 2017-01-03 awesome 166 2017-01-04 awesome 166 2017-01-05 awesome 166 2017-01-06 awesome 166 2017-01-07 awesome
i want write sql query allow me identify date attribute changes user, user's previous attribute, , user's new attribute.
i don't know start.
you can use window function lag
check if current row value matches last row function:
select * ( select t.*, case when attribute <> lag(attribute, 1, attribute) on ( partition uid_ order date_ ) 1 else 0 end flag your_table t ) t flag = 1;
demo
please try not use reserved keywords such uid, date identifiers (i've added underscore _
them in solution).
Comments
Post a Comment