BigQuery 更新在 table 上合并多行
BigQuery Update merge on table with multiple rows
我有以下内容:
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" 错误,因为在 Table 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
-- 这种方法有效,但因为在 SubQuery 中我无法访问 Table 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
关于以通用方式更新数据并以某种方式过滤来自 Table B 的数据并在加入时仅从 goodinfo 获取值“3”的任何想法?
我也在考虑做一个:
WITH filtered_table_b(
select uid, goodinfo from Table B
ORDER BY lastmodifieddate DESC
LIMIT 1
)
但这无济于事,因为我不知何故需要 select 每个用户的基于时间戳的最后一个 goodinfo
谢谢
这是标准 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
这是上面的输出:
我有以下内容:
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" 错误,因为在 Table 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
-- 这种方法有效,但因为在 SubQuery 中我无法访问 Table 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
关于以通用方式更新数据并以某种方式过滤来自 Table B 的数据并在加入时仅从 goodinfo 获取值“3”的任何想法?
我也在考虑做一个:
WITH filtered_table_b(
select uid, goodinfo from Table B
ORDER BY lastmodifieddate DESC
LIMIT 1
)
但这无济于事,因为我不知何故需要 select 每个用户的基于时间戳的最后一个 goodinfo
谢谢
这是标准 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
这是上面的输出: