简体   繁体   English

bigquery 通过时间戳和另一列连接两个表

[英]bigquery join two tables by timestamp and another column

I have two tables as below:我有两个表如下:

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

I would like to join these two tables by host column and timestamp.我想通过主机列和时间戳加入这两个表。 It returns null because usually there is few seconds gap between timestamps so they don't match.它返回 null 因为通常时间戳之间有几秒钟的间隔,所以它们不匹配。 So I want to join them based on host and timestamp(date, hour and minutes only).所以我想根据主机和时间戳(仅限日期、小时和分钟)加入他们。 timestamp column is like this : 2020-11-06 23:33:03.448 UTC时间戳列是这样的: 2020-11-06 23:33:03.448 UTC

I created below query which is based on hour only, I would like to include day, minutes (and maybe second).我创建了以下仅基于小时的查询,我想包括天、分钟(可能还有秒)。

  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)

Use timestamp_trunc() :使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM