【发布时间】:2018-08-03 03:39:06
【问题描述】:
我正在尝试找出前 10 个 UTM 来源的跳出率。表中没有反弹列,所以我必须查询它。我创建了一个查询来查找 TOP 10 UTM 源和另一个查询来查找跳出率。我似乎无法弄清楚如何将这两个查询结合在一起。 数据库表包含:
1) cuuid -cookie ID
2) 会话 - 会话
3) 持续时间
4) 每行代表一个页面浏览量
SELECT
TOP 10 regexp_replace(regexp_substr(url, 'utm_source\\=[^\\&]*'), 'utm_source='),
COUNT(DISTINCT(cuuid)) as "Total Unique Visitors",
COUNT(DISTINCT(session)) as "Total Unique Sessions",
COUNT(*) as "Total Page Views",
CAST(COUNT(DISTINCT(session)) AS FLOAT)/CAST(COUNT(DISTINCT(cuuid)) AS FLOAT) AS "Average Sessions per Visitor",
CAST(COUNT(*) AS FLOAT)/CAST(COUNT(DISTINCT(session)) AS FLOAT) AS "Average Pageview per Session",
ROUND(SUM(CASE WHEN duration < 0 THEN 0 ELSE duration END)::FLOAT/COUNT(DISTINCT(session))) AS "Average Duration per Session"
FROM table1
WHERE url ILIKE '%%utm_source%%'
AND ts>='2018-05-01'
AND ts < '2018-06-01'
GROUP BY 1
ORDER BY 2 DESC;
--add bounce rate query into first--
SELECT
CAST((CAST((SUM(bounces)*100) AS FLOAT)/CAST(COUNT(*) AS FLOAT)) AS VARCHAR(5)) + '%' as "Bounce rate"
FROM (
SELECT
MIN(ts) AS "time_first_viewed",
cuuid,
session,
COUNT(*) as "number_of_events",
CASE WHEN count(*) = 1 THEN 1 ELSE 0 END AS bounces
FROM table1
WHERE ts>='2018-05-01'
AND ts < '2018-06-01'
GROUP BY cuuid, session)
对于最终结果,我需要它在同一个表中。列是:
1)UTM 来源
2)唯一访问者
3)独特的会话
4)页面浏览量
5)会话/访问者
6) 浏览量/会话
7)平均时长
8)跳出率
【问题讨论】:
标签: sql postgresql web-analytics