Google Bigquery如何将lag()函数结果转换为时间戳

时间:2016-02-29 08:14:29

标签: google-bigquery

您可以将滞后函数的结果转换为时间戳。我基本上想要在几秒钟内获得两个时间戳的差异。 使用以下代码,系统告诉我'last_timestamp'的类型未知。当我将鼠标光标放在内部查询的列'last_timestamp'上时,我可以看到它的类型为timestamp。

SELECT clientId, timestamp
  FROM (
        SELECT clientId, timestamp,
               LAG(timestamp,1) OVER
                 (PARTITION BY clientId ORDER BY timestamp)
                 AS last_timestamp
          FROM [oxidation.201602]
       ) last
 WHERE (TIMESTAMP_TO_SEC(timestamp) - TIMESTAMP_TO_SEC(last_timestamp) >= (60 * 30))
    OR last_timestamp IS NULL

1 个答案:

答案 0 :(得分:1)

SELECT clientId, timestamp
FROM (
  SELECT clientId, timestamp, timestamp_sec,
    LAG(timestamp_sec, 1) 
      OVER (PARTITION BY clientId ORDER BY timestamp_sec) AS prev_timestamp_sec
  FROM (
    SELECT clientId, timestamp, TIMESTAMP_TO_SEC(timestamp) as timestamp_sec 
    FROM [oxidation.201602]
  )
) last
WHERE timestamp_sec - prev_timestamp_sec >= 60 * 30
OR prev_timestamp_sec IS NULL