gpt4 book ai didi

mysql - BigQuery 根据最接近的时间戳和匹配值合并表

转载 作者:行者123 更新时间:2023-11-29 07:09:17 24 4
gpt4 key购买 nike

我有两个表,对于表numberTwo的每一行,我需要在表numberOne中获取具有相同提示>cod 值,以及在比较 time1time2 时具有最接近时间的值。

为了更容易理解我需要做的事情是:

表号一:

|  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 | ABC | h323 | 2016-11-03 11:30:00 UTC |
| 3 | ABC | rewq | 2016-11-03 09:00:00 UTC |
| 4 | CDE | abce | 2016-11-03 19:10:00 UTC |

因此,对于表 numberTworow #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 |

处理完每一行后,我将得到一个像这样的表:

所需表格

|  id |  cod  |   hint  |   value  |         time2           |
--------------------------------------------------------------
| 1 | ABC | V | xyz2 | 2016-11-03 18:20:00 UTC |
| 2 | ABC | W | h323 | 2016-11-03 11:30:00 UTC |
| 3 | ABC | W | rewq | 2016-11-03 09:00:00 UTC |
| 4 | CDE | X | abce | 2016-11-03 19:10:00 UTC |

最佳答案

对于 BigQuery 标准 SQL - 请尝试以下操作

您可以使用示例数据取消注释 block 以进行快速测试

WITH 
/*
TableNumberOne AS (
SELECT 1 AS id, 'ABC' AS cod, 'V' AS hint, TIMESTAMP '2016-11-03 18:00:00 UTC' AS time1 UNION ALL
SELECT 2 AS id, 'ABC' AS cod, 'W' AS hint, TIMESTAMP '2016-11-03 12:00:00 UTC' AS time1 UNION ALL
SELECT 3 AS id, 'CDE' AS cod, 'X' AS hint, TIMESTAMP '2016-11-03 19:00:00 UTC' AS time1 UNION ALL
SELECT 4 AS id, 'CDE' AS cod, 'Y' AS hint, TIMESTAMP '2016-11-03 19:30:00 UTC' AS time1 UNION ALL
SELECT 5 AS id, 'EFG' AS cod, 'Z' AS hint, TIMESTAMP '2016-11-03 18:00:00 UTC' AS time1
),
TableNumberTwo AS (
SELECT 1 AS id, 'ABC' AS cod, 'xyz2' AS value, TIMESTAMP '2016-11-03 18:20:00 UTC' AS time2 UNION ALL
SELECT 2 AS id, 'ABC' AS cod, 'h323' AS value, TIMESTAMP '2016-11-03 11:30:00 UTC' AS time2 UNION ALL
SELECT 3 AS id, 'ABC' AS cod, 'rewq' AS value, TIMESTAMP '2016-11-03 09:00:00 UTC' AS time2 UNION ALL
SELECT 4 AS id, 'CDE' AS cod, 'abce' AS value, TIMESTAMP '2016-11-03 19:10:00 UTC' AS time2
),
*/
tempTable AS (
SELECT
t2.id, t2.cod, t2.value, t2.time2, t1.hint,
ROW_NUMBER() OVER(PARTITION BY t2.id, t2.cod, t2.value
ORDER BY ABS(TIMESTAMP_DIFF(t2.time2, t1.time1, SECOND))) AS win
FROM TableNumberTwo AS t2
JOIN TableNumberOne AS t1
ON t1.cod = t2.cod
)
SELECT id, cod, hint, value, time2
FROM tempTable
WHERE win = 1

关于mysql - BigQuery 根据最接近的时间戳和匹配值合并表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40413094/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com