【问题标题】:BigQuery combining data sets using dates and date rangesBigQuery 使用日期和日期范围组合数据集
【发布时间】:2019-02-19 10:52:56
【问题描述】:

有没有办法按序列号组合 2 个表并使用一个表的日期范围和另一个表的日期?

我有 2 个表:table.events 包含跟踪器的事件数据,table.dates 包含跟踪器的操作日期。跟踪器可以多次运行,因此每次打开时都有一个 UID。例如。 Tracker A 可以有 UID A1、A2、A3 等。

如何将 table.events 与诸如

之类的数据结合起来
Date,Serial,Quality,
12/01/2019,A,1,
12/01/2019,B,2,
13/01/2019,C,3,
14/01/2019,A,4,
15/01/2019,A,5,
16/01/2019,B,6,
17/01/2019,B,7,
17/01/2019,C,8,
17/01/2019,B,9

与 table.dates

Start_Date,End_Date,Serial_id,
15/01/2019,18/01/2019,A1,
08/01/2019,14/01/2019,A2,
10/02/2019,18/01/2019,B1,
13/01/2019,16/01/2019,C1,
17/02/2019,18/01/2019,C2

给我一​​个最终结果,例如

Date,Serial,Serial_id,Quality,Start Date,End Date
12/01/2019,A,A1,1,15/01/2019,18/01/2019
12/01/2019,B,B1,2,10/02/2019,18/01/2019
13/01/2019,C,C1,3,13/01/2019,16/01/2019
14/01/2019,A,A1,4,15/01/2019,18/01/2019
15/01/2019,A,A2,5,08/01/2019,14/01/2019
16/01/2019,B,B1,6,10/02/2019,18/01/2019
17/01/2019,B,B1,7,10/02/2019,18/01/2019
17/01/2019,C,C2,8,17/02/2019,18/01/2019
17/01/2019,B,B1,9,10/02/2019,18/01/2019

任何帮助将不胜感激。

编辑:

一个日期将包含多个序列号,所以我也想通过序列号加入。

例如

我有跟踪器 D1 和 E1,它们都在 23/01/2019 进行跟踪。我在 table.events 中有 D 和 E 的单独条目,所以我需要按日期范围和序列 ID 的子字符串进行匹配。

【问题讨论】:

  • 我什么都试过了,还是不行。

标签: sql google-bigquery date-range


【解决方案1】:

你只想要join吗?

select a.*, b.*
from a join
     b
     on b.serial like concat(a.serial, '%') and
        a.date >= b.start_date and a.date <= b.end_date;

【讨论】:

  • 是的,但这不会给我所需的结果。我还想匹配序列号,因为一个日期范围内可以存在多个序列号。
猜你喜欢
  • 2016-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-09
  • 1970-01-01
  • 2017-05-06
相关资源
最近更新 更多