【问题标题】:Bigquery split rows by timestamp of eventBigquery 按事件的时间戳拆分行
【发布时间】:2021-01-29 13:10:24
【问题描述】:

在 Google BigQuery 中,我在单个支持会话中有一个事件列表,这些事件由事件名称标记。每个支持问题resolved_timeresolved 事件的时间戳。每个问题 start_time 是事件 messagependingunresolved 的第一次出现,无论是在会话的绝对开头还是在已解决的标记之后。

换句话说,对话的状态要么是开放的,要么是已解决的。状态在第一次出现 messagependingunresolved 事件时变为打开状态,并在 resolved 事件时关闭。

目前我只有timestampevent_name 的表。想加入start_timeresolved_time

在下面的示例中,这会导致 4 个单独的支持问题。三个正在通过resolved 事件解决,最后一个未解决,因为它没有通过resolved 事件关闭。

timestamp event_name start_time resolved_time
2021-01-15 20:27:59 UTC unresolved 2021-01-15 20:27:59 UTC 2021-01-20 19:31:37 UTC
2021-01-16 03:02:46 UTC message 2021-01-15 20:27:59 UTC 2021-01-20 19:31:37 UTC
2021-01-20 19:31:37 UTC resolved 2021-01-15 20:27:59 UTC 2021-01-20 19:31:37 UTC
2021-01-21 00:13:43 UTC pending 2021-01-21 00:13:43 UTC 2021-01-23 23:38:46 UTC
2021-01-23 23:38:46 UTC resolved 2021-01-21 00:13:43 UTC 2021-01-23 23:38:46 UTC
2021-01-24 00:38:17 UTC message 2021-01-24 00:38:17 UTC 2021-01-24 02:19:44 UTC
2021-01-24 00:42:31 UTC unresolved 2021-01-24 00:38:17 UTC 2021-01-24 02:19:44 UTC
2021-01-24 02:19:44 UTC resolved 2021-01-24 00:38:17 UTC 2021-01-24 02:19:44 UTC
2021-01-25 15:55:50 UTC message 2021-01-25 15:55:50 UTC NULL
2021-01-25 15:59:55 UTC unresolved 2021-01-25 15:55:50 UTC NULL
WITH sample_table AS (
    SELECT  TIMESTAMP("2021-01-15 20:27:59") `timestamp` , "unresolved" event_name UNION ALL
    SELECT  TIMESTAMP("2021-01-16 03:02:46") , "message"  UNION ALL
    SELECT  TIMESTAMP("2021-01-20 19:31:37") , "resolved"  UNION ALL
    SELECT  TIMESTAMP("2021-01-21 00:13:43") , "pending"  UNION ALL
    SELECT  TIMESTAMP("2021-01-23 23:38:46") , "resolved" UNION ALL
    SELECT  TIMESTAMP("2021-01-24 00:38:17") , "message" UNION ALL
    SELECT  TIMESTAMP("2021-01-24 00:42:31") , "unresolved" UNION ALL
    SELECT  TIMESTAMP("2021-01-24 02:19:44") , "resolved" UNION ALL
    SELECT  TIMESTAMP("2021-01-25 15:55:50") , "message" UNION ALL
    SELECT  TIMESTAMP("2021-01-25 15:59:55") , "unresolved" )

SELECT * FROM sample_table
ORDER BY timestamp ASC

提前感谢您的帮助。

【问题讨论】:

  • 老问题总是在新问题打开之前关闭吗?每个问题是否可能有多个同名事件?
  • 不能同时打开两个支持问题。所以状态是打开或关闭。我会在问题中澄清。
  • 我不明白。首先,你想要什么结果?其次,您有两条“已解决”的记录,3 条“消息”、1 条“待处理”和 2 条“未解决”。这如何变成 4 个支持问题?问题不清楚。

标签: sql google-bigquery


【解决方案1】:

说实话,我不明白你想要什么,但是我将消息分组到一个数组中,还计算了票的状态。

WITH sample_table AS (
    SELECT  TIMESTAMP("2021-01-15 20:27:59") `timestamp` , "unresolved" event_name,  TIMESTAMP("2021-01-15 20:27:59") start_time, TIMESTAMP("2021-01-20 19:31:37") resolved_time UNION ALL
    SELECT  TIMESTAMP("2021-01-16 03:02:46") , "message",  TIMESTAMP("2021-01-15 20:27:59"), TIMESTAMP("2021-01-20 19:31:37") UNION ALL
    SELECT  TIMESTAMP("2021-01-20 19:31:37") , "resolved",  TIMESTAMP("2021-01-15 20:27:59"), TIMESTAMP("2021-01-20 19:31:37") UNION ALL
    SELECT  TIMESTAMP("2021-01-21 00:13:43") , "pending",  TIMESTAMP("2021-01-21 00:13:43"), TIMESTAMP("2021-01-23 23:38:46") UNION ALL
    SELECT  TIMESTAMP("2021-01-23 23:38:46") , "resolved",  TIMESTAMP("2021-01-21 00:13:43"), TIMESTAMP("2021-01-23 23:38:46") UNION ALL
    SELECT  TIMESTAMP("2021-01-24 00:38:17") , "message",  TIMESTAMP("2021-01-24 00:38:17"), TIMESTAMP("2021-01-24 02:19:44") UNION ALL
    SELECT  TIMESTAMP("2021-01-24 00:42:31") , "unresolved",  TIMESTAMP("2021-01-24 00:38:17"), TIMESTAMP("2021-01-24 02:19:44") UNION ALL
    SELECT  TIMESTAMP("2021-01-24 02:19:44") , "resolved",  TIMESTAMP("2021-01-24 00:38:17"), TIMESTAMP("2021-01-24 02:19:44") UNION ALL
    SELECT  TIMESTAMP("2021-01-25 15:55:50") , "message",  TIMESTAMP("2021-01-25 15:55:50"), NULL UNION ALL
    SELECT  TIMESTAMP("2021-01-25 15:59:55") , "unresolved",  TIMESTAMP("2021-01-25 15:55:50"), NULL
)
SELECT start_time, resolved_time, 
    array_agg(struct(timestamp, event_name) order by timestamp) as events,
    CASE 
        WHEN MAX('resolved' = event_name) THEN 'resolved'
        ELSE 'open'
    END as status
FROM sample_table
group by 1,2
order by 1,2

【讨论】:

    【解决方案2】:

    在这里移动 MAXMIN 可能很合适:

    WITH sample_table AS (
        SELECT  TIMESTAMP("2021-01-15 20:27:59") timestamp, "unresolved" event_name UNION ALL
        SELECT  TIMESTAMP("2021-01-16 03:02:46"), "message" UNION ALL
        SELECT  TIMESTAMP("2021-01-20 19:31:37"), "resolved" UNION ALL
        SELECT  TIMESTAMP("2021-01-21 00:13:43"), "pending" UNION ALL
        SELECT  TIMESTAMP("2021-01-23 23:38:46"), "resolved" UNION ALL
        SELECT  TIMESTAMP("2021-01-24 00:38:17"), "message" UNION ALL
        SELECT  TIMESTAMP("2021-01-24 00:42:31"), "unresolved" UNION ALL
        SELECT  TIMESTAMP("2021-01-24 02:19:44"), "resolved" UNION ALL
        SELECT  TIMESTAMP("2021-01-25 15:55:50"), "message" UNION ALL
        SELECT  TIMESTAMP("2021-01-25 15:59:55"), "unresolved"
    )
    SELECT 
      *, 
      MAX(IF(prev_event='resolved' OR prev_event IS NULL, timestamp, NULL)) OVER (ORDER BY timestamp ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS start_time,
      MIN(IF(event_name='resolved', timestamp, NULL)) OVER (ORDER BY timestamp ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) AS resolved_time,
    FROM (
      SELECT 
        *,
        LAG(event_name) OVER (ORDER BY timestamp) as prev_event,
      FROM sample_table
    )
    

    【讨论】:

    • 效果很好。我没有想过通过向窗口函数添加条件来做到这一点。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 2020-02-02
    • 2018-08-05
    • 1970-01-01
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多