【发布时间】:2021-12-05 12:13:08
【问题描述】:
我有两张桌子,t1 和 t2。对于 t1 中的每个 id,我在 t2 中有多个记录。我想将 t2 的最近时间戳与 t1 的每条记录相匹配。在 t1 中有一个标志,如果它是 1,我想匹配 t2 的最接近的较小的时间戳,如果它是 0,我想匹配大于 t1 中的最接近的时间戳。 所以我总共有下表: T1 id、标志、时间戳 T2 id,时间戳
有没有有效的方法来做到这一点?
编辑,这里有一些例子:
T1
| customer_id | timestamp_t1 | flag |
|---|---|---|
| 1 | 01.01.21 12:00 | 1 |
| 2 | 01.01.21 13:00 | 0 |
T2
| customer_id | timestamp_t2 | additional attributes |
|---|---|---|
| 1 | 01.01.21 11:00 | attribute1 |
| 1 | 01.01.21 10:00 | attribute2 |
| 1 | 01.01.21 13:00 | attribute3 |
| 2 | 01.01.21 11:00 | attribute4 |
| 2 | 01.01.21 12:00 | attribute5 |
| 2 | 01.01.21 14:00 | attribute6 |
| 2 | 01.01.21 15:00 | attribute7 |
结果:
| customer_id | timetsamp_t1 | timestamp_t2 | flag | additional attributes |
|---|---|---|---|---|
| 1 | 01.01.21 12:00 | 01.01.21 11:00 | 1 | attribute1 |
| 2 | 01.01.21 13:00 | 01.01.21 14:00 | 0 | attribute6 |
我希望这会有所帮助。如你看到的。结果,我们将 T2 的 11:00 与 T1 的 12:00 匹配,因为标志为 1,我们选择了小于 12:00 的最接近的时间戳。我们还将 14:00 与 13:00 进行了匹配,因为标志为 0(因此我们匹配了 id 为 2 且大于 13:00 的最接近的时间戳)。
【问题讨论】:
-
提供样本数据和期望的结果会更有帮助
-
你需要table2的id还是时间戳?时间戳 table2 是否保证唯一? (这是示例数据和所需结果非常有用的一个示例。)
-
感谢您的建议。我添加了一个应该清楚的例子。我注意到这样解释会更好。
-
customer_id 在 T1 中是否唯一?
-
T2 是否有
id列?
标签: sql amazon-redshift amazon-redshift-spectrum