查找条目页面,退出页面并退回-sql

时间:2016-10-23 16:24:47

标签: sql sql-server azure-sql-database

我的表格结构如下:

 Sessionid     Pageurl       timestamp 
     abc1         /testpage1   1465374987308
     abc1         /testpage2   1465375020477
     abc2         /testpage2   1465374987308

我希望创建一个关于每页的输入页数,退出页数和退回计数的报告。

对于任何会话,第一页是输入页面,最后一页是退出页面。

当用户在查看第一页(会话只有一个条目)后离开时会发生跳出

最终报告如下。

 pageurl      EntrypageCount       ExitPagecount       BounceCount
/testpage1     1                     0                   0
/testpage2     1                     2                   1

我已经能够每天获得反弹。 对于跳出,基本选择是..

SELECT sessionid, min(timestamp),CASE WHEN count(*) = 1 THEN 1 ELSE 0 END AS bounces
FROM auditdata GROUP BY sessionid.

但无法弄清楚如何通过pageurl获取它们。

真诚感谢所有帮助。

由于

1 个答案:

答案 0 :(得分:2)

以下是一种方式(demo)。

SELECT Pageurl,
       COUNT(CASE WHEN timestamp = First THEN 1 END) AS EntrypageCount,
       COUNT(CASE WHEN timestamp = Last THEN 1 END) AS ExitPagecount,
       COUNT(CASE WHEN Count = 1 THEN 1 END) AS BounceCount
FROM   (SELECT Pageurl,
               timestamp,
               MIN(timestamp) OVER (PARTITION BY Sessionid) AS First,
               MAX(timestamp) OVER (PARTITION BY Sessionid) AS Last,
               COUNT(*) OVER (PARTITION BY Sessionid)       AS Count
        FROM   auditdata) T
GROUP  BY Pageurl; 

上面使用了大多数现代RDBMS支持的窗口函数,没有版本。

SELECT Pageurl,
       COUNT(CASE WHEN timestamp = First THEN 1 END) AS EntrypageCount,
       COUNT(CASE WHEN timestamp = Last THEN 1 END) AS ExitPagecount,
       COUNT(CASE WHEN Count = 1 THEN 1 END) AS BounceCount
FROM   auditdata a
       JOIN (SELECT Sessionid,
                    MIN(timestamp) AS First,
                    MAX(timestamp) AS Last,
                    COUNT(*)       AS Count
             FROM   auditdata
             GROUP  BY Sessionid) g
         ON a.Sessionid = g.Sessionid
GROUP  BY Pageurl;