Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- select
- t.*
- , LAG(t.activity,1) over w as activity_0
- from (
- -- первая запись
- select
- csh.object_id as categories_id
- , STR_TO_DATE('1900-01-01', '%Y-%m-%d') as date_from
- , IF(csh.status_id IN (0,3,11,15,16,17,20,21,23,24,25,26,27,28,35,36,37,38,39,40,41,42,43), 0, 1) as activity
- from (select object_id
- , min(categories_status_history_id) as categories_status_history_id
- from categories_status_history
- group by object_id) as csh_1
- join categories_status_history as csh on csh_1.object_id = csh.object_id and csh_1.categories_status_history_id = csh.categories_status_history_id
- where csh.object_id = 81856
- union all
- -- остальные записи
- select
- csh.object_id as categories_id
- , date(csh.date_added) as date_from
- , MAX(IF(csh.status_id IN (0,3,11,15,16,17,20,21,23,24,25,26,27,28,35,36,37,38,39,40,41,42,43), 0, 1) ) AS activity
- from categories_status_history as csh
- where csh.object_id = 81856
- group by csh.object_id
- , date(csh.date_added)
- ) as t
- window w as (partition by t.categories_id order by t.date_from)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement