[英]Updating all rows from a column of the same table
首先,很抱歉问这个问题,因为有很多类似的问题。 但我无法根据这些问题和答案更新我的所有行。
所以我有一个关于这个线程的后续问题
新选择的数据 - same_location_count
, same_location_store
,我希望这些数据在同一张表上更新。 就像,我希望它也被更新,而不仅仅是选择。
由此:
store_name | latitude | longitude | store_id | same_location_count | same_location_store_id
SR Restaurant and Cafe | -41.575449 | 147.16824 | 1112 | 0 | null
Big Bite Burgers | -41.575449 | 147.16824 | 1113 | 0 | null
Amigos | -41.575449 | 147.16824 | 1114 | 0 | null
Domino's | -38.33983 | 143.58384 | 1115 | 0 | null
对此:
store_name | latitude | longitude | store_id | same_location_count | same_location_store_id
SR Restaurant and Cafe | -41.575449 | 147.16824 | 1112 | 2 | 1113:1114
Big Bite Burgers | -41.575449 | 147.16824 | 1113 | 2 | 1112:1114
Amigos | -41.575449 | 147.16824 | 1114 | 2 | 1112:1113
Domino's | -38.33983 | 143.58384 | 1115 | 0 | null
这是适用于我的 select 语句:
SELECT
*,
(COUNT(id) OVER (PARTITION BY lat, long))-1 AS same_location_count,
REPLACE( STRING_AGG(CONCAT(id,':'),'') OVER (PARTITION BY lat, long), CONCAT(id,':'), '') AS same_location_store_id
FROM
test
现在,到下一步,我将如何使用这些选定的数据更新表格?
您可以尝试使用UPDATE FROM
update T set same_location_count = tx.same_location_count, same_location_store_id = tx.same_location_store_id
from test t
join (
SELECT
*,
(COUNT(id) OVER (PARTITION BY lat, long))-1 AS same_location_count,
REPLACE( STRING_AGG(CONCAT(id,':'),'') OVER (PARTITION BY lat, long), CONCAT(id,':'), '') AS same_location_store_id
FROM
test
) tx on t.id = tx.id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.