--
---- psql -c "select levelid, pl.pathid, path from pathlevel pl, path pa where pl.levelid=(select pathid from path where path='/test/testbasen/') AND pa.pathid=pl.pathid order by path"
--
---- psql -c "select slashcount, levelid, pl.pathid, path from pathlevel pl, path pa where pl.levelid=(select pathid from path where path='/test/testbasen/') AND pa.pathid=pl.pathid order by slashcount,path"
--
--select
--	count(h.pathid) AS "cnt",
--	path
--from
--	httplog h,
--	path pa 
--where	
--	h.pathid=pa.pathid
--group by
--	pa.path
--order by	
--	cnt desc
--limit	10
--;
--
--select
--	count(h.pathid),
--	pa.path
--from
--	httplog h,
--	path pa,
--	pathlevel pl
--where
--	h.pathid=pl.levelid
--	and h.pathid=pa.pathid
--	and pa.slashcount=3
--group by
--	pa.path
--limit 10
--;
--
--select
--	count(h.pathid)
--from
--	httplog h
--where	
--	h.pathid in (
--		select pathid from pathlevel where levelid=(
--			select pathid from path where path='/test/testbasen/'
--		)
--	)
--;
--
--select
--	count(levelid) AS "subdir count",
--	pa.pathid,
--	path
--from
--	path pa,
--	pathlevel pl
--where
--	slashcount = 3
--	and IsADir
--	and pa.pathid=pl.levelid
--group by
--	pa.pathid,
--	pa.path
--order by
--	path
--limit 50
--;

--explain
--select
--	count(h.httplogid) as "hitcount",
--	pa.path
--from
--	httplog h,
--	path pa,
--	pathlevel pl
--where
--	pa.slashcount = 3
--	--and h.pathid in (select pathid from pathlevel where levelid=pa.pathid)
--	and h.pathid = pl.pathid 
--	and pl.levelid=pa.pathid
--	and hitdatetime>'2005-01-03 00:00:01+01'
--	and hitdatetime<'2005-01-04 06:00:01+01'
--group by
--	pa.path
--order by
--	hitcount desc
--limit	20
--;

--explain
--CREATE FUNCTION getfoo(int) RETURNS SETOF foo AS $$
--    SELECT * FROM foo WHERE fooid = $1;
--    $$ LANGUAGE SQL;
-- http://www.postgresql.org/docs/8.0/static/xfunc-sql.html
select

	count(ses) as "count",
	path
from (
	select
		h.sessionid as "ses",
		pa.path
	from
		httplog h,
		path pa,
		pathlevel pl
	where
		pa.slashcount = 3
		and h.pathid = pl.pathid 
		and pl.levelid=pa.pathid
		and hitdatetime>'2005-01-03 00:00:01+01'
		and hitdatetime<'2005-01-04 06:00:01+01'
	group by
		h.sessionid,
		pa.path
) as "foo"
group by
	path
order by
	count desc
limit	20
;
