【问题标题】:bigquery join two tables by timestamp and another columnbigquery 通过时间戳和另一列连接两个表
【发布时间】:2020-11-13 13:03:39
【问题描述】:

我有两张表如下:

TableA: host, timestamp, type
TableB: host, timestamp, type

我想按主机列和时间戳连接这两个表。它返回 null 因为通常时间戳之间有几秒钟的间隔,所以它们不匹配。所以我想根据主机和时间戳(仅限日期、小时和分钟)加入他们。 时间戳列是这样的:2020-11-06 23:33:03.448 UTC

我在下面创建了仅基于小时的查询,我想包括天、分钟(可能还有秒)。

  with tableA as (
      SELECT
      EXTRACT(HOUR from timestamp) as hour,
      ...
  ),
  tableB as (
      SELECT
      EXTRACT(HOUR from timestamp) as hour,
      ...
  )
  SELECT * 
  FROM tableA 
  JOIN tableB 
  USING(host,hour)

【问题讨论】:

    标签: sql google-bigquery


    【解决方案1】:

    使用timestamp_trunc():

    with tableA as (
          SELECT TIMESTAMP_TRUNC(timestamp, HOUR ) as hour,
                 ...
         ),
         tableB as (
          SELECT TIMESTAMP_TRUNC(timestamp, HOUR ) as hour,
                 ...
         )
    SELECT * 
    FROM tableA JOIN
         tableB 
         USING(host, hour);
    

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 2021-12-08
      • 2021-09-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-28
      相关资源
      最近更新 更多