【问题标题】:Combine 2 SQL query (PostgreSQL)结合2条SQL查询(PostgreSQL)
【发布时间】: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


    【解决方案1】:

    您只需使用逗号执行以下操作: 希望这有效

    Select * from 
    (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)table1
    ,
    (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))table2
    

    您为列定义别名并完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-31
      相关资源
      最近更新 更多