・user_idごとにパーティションで分けます。
・start_timestampを昇順に並べ替えたものを利用します。
・action_idが-1のレコードではget_idは-1以外になります。
・action_idが-1のレコードのstart_timestampとend_timestampは同値です。
- user_idごとにパーティションで分けます。
- start_timestampを昇順に並べ替えたものを利用します。
- action_idが-1のレコードではget_idは-1以外になります。
- action_idが-1のレコードのstart_timestampとend_timestampは同値です。
select
user_id
,start_timestamp
,end_timestamp
,action_id
,get_id
from
(
select '1234' as user_id, '2017-03-10 09:12:31.881' as start_timestamp,'2017-03-10 09:12:31.881' as end_timestamp,'a1' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id, '2017-03-10 09:13:25.709' as start_timestamp,'2017-03-10 09:15:07.805' as end_timestamp,'a2' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:15:07.805' as start_timestamp,'2017-03-10 10:08:18.762' as end_timestamp,'a3' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:15:07.805' as start_timestamp,'2017-03-10 09:15:07.805' as end_timestamp,'a4' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:24:44.440' as start_timestamp,'2017-03-10 09:24:44.440' as end_timestamp,'a5' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:24:44.440' as start_timestamp,'2017-03-10 09:24:54.450' as end_timestamp,'a6' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:24:54.450' as start_timestamp,'2017-03-10 09:25:01.457' as end_timestamp,'a7' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:25:01.457' as start_timestamp,'2017-03-10 09:25:01.457' as end_timestamp,'a8' as action_id ,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:32:53.000' as start_timestamp,'2017-03-10 09:32:53.000' as end_timestamp,'-1' as action_id ,'3' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:44:03.500' as start_timestamp,'2017-03-10 09:49:08.473' as end_timestamp,'a10' as action_id,'-1' as get_id from dual union all
select '1234' as user_id,'2017-03-10 09:45:53.000' as start_timestamp,'2017-03-10 09:45:53.000' as end_timestamp,'-1' as action_id ,'4' as get_id from dual union all
select '2345' as user_id,'2017-03-10 09:44:03.500' as start_timestamp,'2017-03-10 09:49:08.473' as end_timestamp,'a1' as action_id,'-1' as get_id from dual union all
select '2345' as user_id,'2017-03-10 19:44:03.500' as start_timestamp,'2017-03-10 19:49:08.473' as end_timestamp,'a2' as action_id,'-1' as get_id from dual union all
select '2345' as user_id,'2017-03-10 19:45:03.500' as start_timestamp,'2017-03-10 19:45:03.473' as end_timestamp,'a3' as action_id,'-1' as get_id from dual union all
select '2345' as user_id,'2017-03-10 19:45:05.500' as start_timestamp,'2017-03-10 19:45:05.500' as end_timestamp,'-1' as action_id,'11' as get_id from dual
) a
order by user_id,start_timestamp
;
select
user_id
,start_timestamp
,end_timestamp
,action_id
,get_id
,in_action_id
from
(
select '1234' as user_id, '2017-03-10 09:12:31.881' as start_timestamp,'2017-03-10 09:12:31.881' as end_timestamp,'a1' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id, '2017-03-10 09:13:25.709' as start_timestamp,'2017-03-10 09:15:07.805' as end_timestamp,'a2' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:15:07.805' as start_timestamp,'2017-03-10 10:08:18.762' as end_timestamp,'a3' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:15:07.805' as start_timestamp,'2017-03-10 09:15:07.805' as end_timestamp,'a4' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:24:44.440' as start_timestamp,'2017-03-10 09:24:44.440' as end_timestamp,'a5' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:24:44.440' as start_timestamp,'2017-03-10 09:24:54.450' as end_timestamp,'a6' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:24:54.450' as start_timestamp,'2017-03-10 09:25:01.457' as end_timestamp,'a7' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:25:01.457' as start_timestamp,'2017-03-10 09:25:01.457' as end_timestamp,'a8' as action_id ,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:32:53.000' as start_timestamp,'2017-03-10 09:32:53.000' as end_timestamp,'-1' as action_id ,'13' as get_id ,'a3' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:44:03.500' as start_timestamp,'2017-03-10 09:49:08.473' as end_timestamp,'a10' as action_id,'-1' as get_id ,'-1' as in_action_id from dual union all
select '1234' as user_id,'2017-03-10 09:45:53.000' as start_timestamp,'2017-03-10 09:45:53.000' as end_timestamp,'-1' as action_id ,'24' as get_id ,'a10' as in_action_id from dual union all
select '2345' as user_id,'2017-03-10 09:44:03.500' as start_timestamp,'2017-03-10 09:49:08.473' as end_timestamp,'a1' as action_id,'-1' as get_id ,'-1' as in_action_id from dual union all
select '2345' as user_id,'2017-03-10 19:44:03.500' as start_timestamp,'2017-03-10 19:49:08.473' as end_timestamp,'a2' as action_id,'-1' as get_id ,'-1' as in_action_id from dual union all
select '2345' as user_id,'2017-03-10 19:45:03.500' as start_timestamp,'2017-03-10 19:45:03.473' as end_timestamp,'a3' as action_id,'-1' as get_id ,'-1' as in_action_id from dual union all
select '2345' as user_id,'2017-03-10 19:45:05.500' as start_timestamp,'2017-03-10 19:45:05.500' as end_timestamp,'-1' as action_id,'11' as get_id ,'a2' as in_action_id from dual
) a
order by user_id,start_timestamp
;