Skip to main content
表 dual を使っているのでタグに oracle を追加
リンク
編集済みの本文
ソース リンク
cubick
  • 2.1万
  • 5
  • 27
  • 64

・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
;

・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
;
  • 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
;
ソース リンク
hmrex
  • 45
  • 4

SQLのwindow関数について

SQLで特定の条件を満たすレコードを抽出するSQL query を教えてください。
(ユーザーごとにパーティションで分けたいためwindow関数を利用するかと思います。)

データセットは以下の通りとなります。

・user_idごとにパーティションで分けます。
・start_timestampを昇順に並べ替えたものを利用します。
・action_idが-1のレコードではget_idは-1以外になります。
・action_idが-1のレコードのstart_timestampとend_timestampは同値です。

抽出したいレコードですが、
action_idが-1のレコードのstart_timestampが直前のどのaction_idのstart_timestampとend_timestampに含まれるか、含まれるaction_idを別列として定義したい。

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
;

最終的に得られる結果は以下のようになると思います。
新たに定義したin_action_idの-1はnullでも構いません。

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
;