Last active
August 29, 2015 14:19
-
-
Save aamine/897cdb165e8b835adc95 to your computer and use it in GitHub Desktop.
セッショナイズされたアクセスログから部分パスの出現回数を集計する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table sessionized_access_log | |
( session_id int | |
, session_seq int | |
, log_time timestamp | |
, pv_signature text -- 1アクセスを表現する値。pathとか | |
); | |
-- 長さ3の部分パスの出現回数を集計(ウィンドウ関数使わない編) | |
-- partitionから任意長のウィンドウフレームを自力で切り出すためにも使えるぞ! | |
select | |
partial_path | |
, count(*) | |
from ( | |
select | |
array_agg(a.pv_signature order by session_seq) as partial_path | |
from | |
sessionized_access_log as a | |
cross join generate_series(1,3) as s (seq) | |
group by | |
a.session_id | |
, a.session_seq + s.seq | |
) tmp | |
group by | |
partial_path | |
; | |
-- 長さ3の部分パスの出現回数を集計(PostgreSQLのウィンドウ関数使う) | |
-- session_seqいらなかった。 | |
select | |
partial_path | |
, count(*) | |
from ( | |
select | |
array_agg(pv_signature) over ( | |
partition by session_id | |
order by log_time | |
rows between 2 preceding and current row | |
) as partial_path | |
from | |
sessionized_access_log | |
) t | |
group by | |
partial_path | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment