BigQuery 更新在具有多行的表上合并
Posted
技术标签:
【中文标题】BigQuery 更新在具有多行的表上合并【英文标题】:BigQuery Update merge on table with multiple rows 【发布时间】:2019-12-17 13:46:05 【问题描述】:我有以下几点:
Table A:
|uid|info|..
|123|null|..
Table B:
|uid|goodinfo|timestamp|
|123 | 3 |2019-12-12
|123 | 5 |2019-01-12
|234 | 11 |2019-10-12
当我尝试运行更新语句时,我总是收到“UPDATE/MERGE must match at most one source row for each target row”错误,因为在表 B 中我得到了多行并且我没有任何方法进行连接比这更具体。
我试过了:
UPDATE `Table A` a
SET info = (select goodinfo from `Table B` where uid=123
ORDER BY lastmodifieddate DESC
LIMIT 1) b
WHERE
a.info IS NULL AND
a.user_id=123
-- 这种方法有效,但因为在子查询中我无权访问表 A,我无法将其概括为:
SET info = (select goodinfo from `Table B` where uid=a.uid
ORDER BY lastmodifieddate DESC
LIMIT 1) b
-- 这会抛出一个错误,说他不知道“a.uid”是谁
然后我尝试使用 BigQuery 中的合并:
MERGE `Table A` a
USING (
select goodinfo,uid from `Table B`
ORDER BY lastmodifieddate DESC
LIMIT 1
) b
ON a.uid = b.uid
WHEN MATCHED and a.info is null and DATE(a.timestamp) = "2019-12-12" THEN
UPDATE SET a.info = b.goodinfo
-- 此查询实际上成功完成,但由于我尚未找到的原因未修改任何行
然后我试过了:
UPDATE `Table A` a
SET a.info = b.goodinfo
FROM `Table B` b
WHERE a.uid = b.uid
and DATE(a.timestamp) = "2019-12-12"
and a.info IS NULL
//here I get the same error and I cannot filter the data from Table B and get the same error
关于以通用方式更新数据并以某种方式过滤表 B 中的数据并在加入时仅从 goodinfo 中获取值“3”的任何想法?
我也在考虑做一个:
WITH filtered_table_b(
select uid, goodinfo from Table B
ORDER BY lastmodifieddate DESC
LIMIT 1
)
但这无济于事,因为我不知何故需要根据每个用户的时间戳选择最后一个好信息
谢谢
【问题讨论】:
【参考方案1】:这是您可以使用的标准 SQL:
WITH data AS (
select '123' as uid, 3 as goodinfo, DATE('2019-12-12') as timestamp union all
select '123' as uid, 5 as goodinfo, DATE('2019-01-12') as timestamp union all
select '234' as uid, 11 as goodinfo, DATE('2019-10-12') as timestamp
),
filterData AS (
select uid, max(timestamp) maxTimestamp from data
group by uid
)
select data.uid, goodinfo, filterData.maxTimestamp as maxTimestamp
from data inner join filterData on data.uid = filterData.uid and data.timestamp = filterData.maxTimestamp
这是上面的输出:
【讨论】:
以上是关于BigQuery 更新在具有多行的表上合并的主要内容,如果未能解决你的问题,请参考以下文章