设置布尔值作为mysql记录,指示在选定的时间戳范围之前是否存在其他记录

时间:2019-01-14 20:16:34

标签: mysql boolean-logic

基本表类似于:

mysqlTable:
          visitorID   ,park          ,DateTimeStamp
          8369        ,Birmingham    ,12/27/2018 03:26:38 PM
          8369        ,Birmingham    ,12/28/2018 11:27:32 AM
          8828        ,Central       ,01/02/2019 10:01
          8828        ,Central       ,01/04/2019 9:50
          8825        ,Central       ,12/21/2018 09:47:27 AM
          8821        ,Central       ,12/26/2018 10:11:40 AM
          8821        ,Central       ,02/03/2019 10:00:59 AM
          8821        ,Central       ,01/02/2019 10:04
          88281       ,Central       ,01/04/2019 9:53

从此表中,我正在创建一个新的表,其中我在特定公园中对访客ID进行计数,然后按特定时间段内的visitorID和访问日期对访问进行分组。

mysql query: 

SELECT COUNT(*)AS visits,dateTimeStamp,visitorID 
FROM parkVisits 
WHERE 
    dateTimeStamp BETWEEN '2019-01-01 00:00:01' AND '2019-01-04 23:59:59'
    AND park ='Central'
GROUP BY visitorID, CAST(dateTimeStamp AS DATE);

我的结果:

mysql table:

visits   ,dateTimeStamp     ,visitorID   
2        ,01/02/2019 10:01  ,8828      
1        ,01/02/2019 10:04  ,8821       
1        ,01/04/2019 9:53   ,88281      

我想有一列,其中的布尔值指示访问者是否在行的指定日期之前的任何时间访问过。 我当时正在考虑将表格上最早的dateTimeStamp与最早的给定日期进行比较,但是可能会发生在给定期间内首次访问和回访的情况。

预期:

mysql table:

visits   ,dateTimeStamp   ,visitorID    ,returningVisitor
2        ,01/02/2019 10:01,  8828       ,TRUE
1        ,01/02/2019 10:04,  8821       ,FALSE
1        ,01/04/2019 9:53,   88281      ,FALSE

编辑:

我正在使用MySQL 5.6.40

1 个答案:

答案 0 :(得分:1)

假设您正在运行MySQL 8.0,则可以使用带有窗口函数FIRST_VALUE的内部查询来获取分析期间首次访问的时间戳。然后在外部查询中,带有子查询的EXISTS子句可用于检查当前访问者是否曾经访问过同一公园。

SELECT 
    x.visits,
    x.dateTimeStamp,
    x.visitorID,
    EXISTS (
        SELECT 1 FROM parkVisits WHERE park = x.park AND visitorID = x.visitorID AND dateTimeStamp < x.dateTimeStamp
    ) returningVisitor
FROM (
    SELECT DISTINCT
        COUNT(*) OVER (PARTITION BY p.visitorID) visits,
        FIRST_VALUE(p.dateTimeStamp) OVER (PARTITION BY p.visitorID ORDER BY p.dateTimeStamp) dateTimeStamp,
        p.visitorID,
        p.park
    FROM parkVisits p
    WHERE
        p.dateTimeStamp BETWEEN '2019-01-01 00:00:01' AND '2019-01-04 23:59:59' 
        AND p.park ='Central'
) x
ORDER BY 1 desc, 2

this db fiddle中包含您的示例数据,它返回:

| visits | dateTimeStamp       | visitorID | returningVisitor |
| ------ | ------------------- | --------- | ---------------- |
| 2      | 2019-01-02 10:01:00 | 8828      | 0                |
| 1      | 2019-01-02 10:04:00 | 8821      | 1                |
| 1      | 2019-01-04 09:53:00 | 88281     | 0                |

注意:我认为回访者是8821,而不是您的问题所示的8828

如果运行的是不支持窗口功能的较低版本的MySQL,则可以在子查询中使用GROUP BY子句,例如:

SELECT 
    x.visits,
    x.dateTimeStamp,
    x.visitorID,
    EXISTS (
        SELECT 1 FROM parkVisits WHERE park = x.park AND visitorID = x.visitorID AND dateTimeStamp < x.dateTimeStamp
    ) returningVisitor
FROM (
    SELECT DISTINCT
        COUNT(*) visits,
        MIN(p.dateTimeStamp) dateTimeStamp,
        p.visitorID,
        p.park
    FROM parkVisits p
    WHERE
        p.dateTimeStamp BETWEEN '2019-01-01 00:00:01' AND '2019-01-04 23:59:59' 
        AND p.park ='Central'
    GROUP BY p.visitorID, p.park
) x
ORDER BY 1 desc, 2   

请参见this db fiddle