1

我们虔诚地使用 BigQuery,并且有两个表基本上是由不同的进程并行更新的。我遇到的问题是我们没有表的唯一标识符,目标是尽可能将这两个表与零重复结合起来。唯一标识符是两列的组合。

我尝试了各种基于 MySQL 的查询,但似乎没有一个在 BigQuery 中有效。所以我在这里发帖寻求帮助。:)

步骤 1. 将“干净”表复制到新的合并表中。

步骤 2. 查询“脏”(旧)表并插入任何缺失的条目。

查询尝试 1:

SELECT
  COUNT(c.*)
FROM
  [flash-student-96619:device_data.device_datav3_20160530] AS old
WHERE NOT EXISTS (
  SELECT
    1
  FROM
    [flash-student-96619:device_data_v7_merged.20160530] AS new
  WHERE
    new.dsn = old.dsn
    AND new.timestamp = old.timestamp 
)

错误:错误:6.1 - 10.65。一次只能执行一个查询。

查询尝试 2:

SELECT
  *
FROM
  [flash-student-96619:device_data.device_datav3_20160530]
WHERE
  (dsn, timestamp) NOT IN (
  SELECT
    dsn,
    timestamp
  FROM
    [flash-student-96619:device_data_v7_merged.20160530] 
  )

错误:在第 6 行第 7 列遇到“”、“”、“”。期待:“)”...

老实说,如果我能在一个查询中做到这一点,我会很高兴的。我需要从两个表中获取,并使用唯一数据创建一个新表。

有什么帮助吗?

4

2 回答 2

5

像下面这样的东西应该工作

SELECT * 
FROM (
  SELECT *,
    ROW_NUMBER() OVER(PARTITION BY dsn, timestamp) AS dup
  FROM
    [flash-student-96619:device_data.device_datav3_20160530],
    [flash-student-96619:device_data_v7_merged.20160530] 
) 
WHERE dup = 1  

我建议在外部 SELECT 中使用明确的字段列表而不是 *,这样您就可以从实际输出中省略 dup

于 2016-07-18T22:02:57.833 回答
1

有点晚了,但我想指出您的原始查询使用标准 SQL进行了少量修改(取消选中“显示选项”下的“使用旧版 SQL”框)。我只需要更改new为其他内容,因为这是保留关键字。例如,这个查询是有效的:

WITH OldData AS (
  SELECT
    x AS dsn,
    TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL x HOUR) AS timestamp
  FROM UNNEST([1, 2, 3, 4]) AS x),
NewData AS (
  SELECT
    x AS dsn,
    TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL x HOUR) AS timestamp
  FROM UNNEST([5, 2, 1, 6]) AS x)
SELECT
  COUNT(*)
FROM OldData oldData
WHERE NOT EXISTS (
  SELECT 1
  FROM NewData newData
  WHERE
    newData.dsn = oldData.dsn
    AND newData.timestamp = oldData.timestamp
);
+-----+
| f0_ |
+-----+
|   2 |
+-----+

关于您的第二次尝试,您可以执行以下操作:

WITH OldData AS (
  SELECT
    x AS dsn,
    TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL x HOUR) AS timestamp
  FROM UNNEST([1, 2, 3, 4]) AS x),
NewData AS (
  SELECT
    x AS dsn,
    TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL x HOUR) AS timestamp
  FROM UNNEST([5, 2, 1, 6]) AS x)
SELECT
  *
FROM OldData
WHERE
  STRUCT(dsn, timestamp) NOT IN (
  SELECT AS STRUCT
    dsn,
    timestamp
  FROM NewData);
+-----+---------------------+
| dsn |      timestamp      |
+-----+---------------------+
|   3 | 2016-07-21 11:54:08 |
|   4 | 2016-07-21 10:54:08 |
+-----+---------------------+
于 2016-07-21T14:55:02.873 回答