简体   繁体   English

什么是行的 bigquery sql 内连接等效项?

[英]What is the bigquery sql inner join equivalent for rows?

I have the following 2 tables:我有以下 2 个表: 在此处输入图像描述

I would like to create a table where我想创建一个表

  • all rows of table 1 are included包括表 1 的所有行
  • if the timestamp of any row of table 2 falls in between the timestamp and endTime of any row of table 1, then include the row.如果表 2 的任何行的时间戳介于表 1 的任何行的时间戳和 endTime 之间,则包括该行。

The resultant table would like:结果表如下:
在此处输入图像描述

There are columns/fields that are common to both tables, but I haven't included them for brevity.有两个表共有的列/字段,但为简洁起见,我没有包括它们。 Basically, I am looking for the equivalent of an inner join operation but then instead of adding the rows of table 2 as columns, add them as rows.基本上,我正在寻找等效于内部连接操作的方法,但不是将表 2 的行添加为列,而是将它们添加为行。 I have written a sample code whilst experimenting with inner join as below:我在尝试内部连接时编写了一个示例代码,如下所示:

WITH table_a AS (
  SELECT 'x' AS event, 1 AS timestamp, 5 AS endtime, 'a' AS field1
  UNION ALL SELECT 'x', 100, 200, 'b'
),
table_b AS (
  SELECT 'y' AS event, 2 AS timestamp, 'm' AS field2
  UNION ALL SELECT 'y', 25, 'n'
  UNION ALL SELECT 'y', 150, 'o'
)

SELECT
  table_a.*,
  table_b.*
FROM table_a JOIN table_b 

Any thoughts what bigquery sql functions I can use?任何想法我可以使用什么 bigquery sql 功能?

Use below下面使用

select *, null field2 from table_a  union all
select distinct b.event, b.timestamp, null, cast(null as string), field2 
from table_b b
join table_a a 
on b.timestamp between a.timestamp and a.endtime      

if applied to sample data in your question - output is如果应用于您问题中的示例数据 - output 是

在此处输入图像描述

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

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