【问题标题】:Updating all rows from a column of the same table更新同一个表的列中的所有行
【发布时间】:2021-11-18 13:20:29
【问题描述】:

首先,很抱歉提出这个问题,因为有很多类似的问题。但我无法根据这些问题和答案更新我的所有行。

所以我有一个关于这个thread的后续问题

新选择的数据——same_location_countsame_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
  *,
  (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

现在,进入下一步,我将如何使用这些选定的数据更新表格?

【问题讨论】:

标签: sql google-bigquery


【解决方案1】:

你可以试试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

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2020-10-04
    • 1970-01-01
    • 2023-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多