【发布时间】:2016-11-30 19:57:01
【问题描述】:
类似于提问BigQuery combine tables based on closest timerstamp and matching value
我有三个表,对于表 numberTwo 的每一行,我需要在表 numberOne 中获取具有相同 的 hint >cod 值以及在比较 time1 和 time2 时具有 最接近时间 的值。如果 cod 未显示在表 numberOne 中,它会尝试获取与表 中的 cod 匹配的 hint numberThree
为了更容易理解我需要做的是:
表号一:
| id | cod | hint | time1 |
---------------------------------------------------
| 1 | ABC | V | 2016-11-03 18:00:00 UTC |
| 2 | ABC | W | 2016-11-03 12:00:00 UTC |
| 3 | CDE | X | 2016-11-03 19:00:00 UTC |
| 4 | CDE | Y | 2016-11-03 19:30:00 UTC |
| 5 | EFG | Z | 2016-11-03 18:00:00 UTC |
表号二
| id | cod | value | time2 |
----------------------------------------------------
| 1 | ABC | xyz2 | 2016-11-03 18:20:00 UTC |
| 2 | FHK | h323 | 2016-11-03 11:30:00 UTC |
| 3 | ABC | rewq | 2016-11-03 09:00:00 UTC |
| 4 | IJK | abce | 2016-11-03 19:10:00 UTC |
表号三
| id | cod | hint |
--------------------------
| 1 | FHK | tes1 |
| 2 | IJK | tes2 |
| 3 | MNK | tes3 |
| 4 | MOP | tes4 |
因此,对于表 numberTwo 的 row #1,我将使用 cod: ABC 获取表 numberOne 中的所有行强>
| 1 | ABC | V | 2016-11-03 18:00:00 UTC |
| 2 | ABC | W | 2016-11-03 12:00:00 UTC |
在这两者之间,我会得到一个与 time2 最接近的时间戳:
| 1 | ABC | V | 2016-11-03 18:00:00 UTC |
如果 cod 未显示在表 numberOne 中,则它与表 numberThree 匹配。 numberOne 和 numberThree 中的代码是唯一的。因此,不会在两个表中显示相同的代码。所以它可以先尝试匹配表 numberThree。
处理完每一行后,我会得到一个像这样的表格:
所需的表
| id | cod | hint | value | time2 |
--------------------------------------------------------------
| 1 | ABC | V | xyz2 | 2016-11-03 18:20:00 UTC |
| 2 | FHK | tes1 | h323 | |
| 3 | ABC | W | rewq | 2016-11-03 09:00:00 UTC |
| 4 | IJK | tes2 | abce | |
【问题讨论】:
-
numberThree 的架构是什么?什么是预期的输出模式? + 请举个简单的例子!
-
@MikhailBerlyant 我已经更新了问题。
-
这项工作成功了吗?
标签: mysql sql google-bigquery