--#!/usr/bin/perl

select HostNameID from hostname where hostname = 'schou.dk';

\set SERVERNAMEID 1604
\set NIVO 2

select
	pathid,
	'"'|| substring(path, '^((/[^/]+){0,'||:NIVO||'}(/|$))') ||'"' as "subdir_niveau"
    into temp table gruppe
    from path
--    where path ~ '/.*/.*/.*/.*'
    ;

-- Antal sessioner på en url gruperet på uge
select distinct sessionid, 
    extract(year from hitDateTime) || '-' ||
    extract(week from hitDateTime) as "ugenr",
    gruppe.subdir_niveau
    into temp distinct_session
    from gruppe, httplog
    where httplog.hostid=(select hostnameid from hostname where hostname='www.forbrug.dk') --:SERVERNAMEID -- www.fi.dk
    and gruppe.pathid = httplog.pathid
    and hitdatetime > '2004-08-14'
    and hitdatetime < '2004-08-15' 
    ;

select count(sessionid), ugenr, subdir_niveau
    from distinct_session
    group by "ugenr", "subdir_niveau"
    order by "subdir_niveau","ugenr"
    ;


\q
-- Antal hit på en url gruperet på uge
-- select count(httplogid) as cnt, 
--     extract(year from hitDateTime) || '-' ||
--     extract(week from hitDateTime) as "ugenr",
--     gruppe.subdir_niveau
--     into temp table id_table
--     from gruppe, httplog
--     where httplog.servernameid=183 -- www.fi.dk
--     and gruppe.pathid = httplog.pathid
--     and hitdatetime > '2004-01-01'
--     and hitdatetime < '2004-01-31' 
--     group by "ugenr", "subdir_niveau";

select * from id_table;
\q
select ugenr, cnt, path from id_table, path
    where path.pathid=id_table.pathid;


# count, ugenr, url

