
SELECT
	(COUNT(DISTINCT h.sessionid)) AS count,
	(hostname || substring(path from '^((/[^/]+){1,2})')) AS paqu
FROM
	httplog h,
	hostname hn,
	path pa,
	query qu
WHERE
	(hitDateTime>('NOW'::TIMESTAMP WITH TIME ZONE-'600 MIN'::INTERVAL)
	AND hitDateTime<('NOW'::TIMESTAMP WITH TIME ZONE))
	AND (h.contenttypeid IN (SELECT contenttypeid FROM contenttype WHERE canlink='t'))
	AND (h.statusid!=404)
	AND (h.refererid IN (
		SELECT refererid FROM referer rf WHERE rf.hostnameid=(
			SELECT hostnameid FROM hostname WHERE hostname='www.forbrug.dk'
		) AND rf.pathid IN (
			SELECT pathid FROM path WHERE path LIKE '/mad/sundmad1%'
		)
	))
	AND (h.servernameid=hn.hostnameid AND h.pathid=pa.pathid AND h.queryid=qu.queryid)
GROUP BY paqu ORDER BY count DESC LIMIT 20
