结合2条SQL查询(PostgreSQL)

时间:2018-08-03 03:39:07

标签: sql postgresql web-analytics

我正在尝试找出前10个UTM来源的跳出率。表格中没有用于反弹的列,因此我必须对其进行查询。我创建了一个查询以查找TOP 10 UTM源,并创建了另一个查询以查找跳出率。我似乎无法弄清楚如何将这两个查询组合在一起。 数据库表包含:

  

1)提示-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)跳出率

1 个答案:

答案 0 :(得分:0)

您只需要像下面这样用逗号: 希望这行得通

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

您为列定义别名并完成。