sql - 合并在与另一个表的关系中使用的重复表行
问题描述
我有以下表结构:
table_a
id | customer_id | product_id
---+-------------+------
1 | c1 | p1
2 | c1 | p1
3 | c2 | p1
table_b
id | table_a_id | attribute
---+-------------+------
99 | 1 | a1
98 | 2 | a2
97 | 3 | a3
如您所见table_a
,有重复值,我想合并它们。不幸的table_a
PK
是,也用于table_b
.
最终结果应该是:
table_a
id | customer_id | product_id
---+-------------+------
1 | c1 | p1
3 | c2 | p1
table_b
id | table_a_id | attribute
---+-------------+------
99 | 1 | a1
98 | 1 | a2
97 | 3 | a3
我必须更新与 的table_b
关系table_a
,然后清除所有未使用的键table_a
。
不幸的是,我想到的唯一查询真的很重,并且可以完成之前的数据库超时。table_a
拥有 200k+ 条记录,并且table_b
至少是其两倍。
我的想法是:
- 加入
table_a
,table_b
以获得:(table_b_id, table_a_customer_id, table_a_product_id)
- 获取
table_a
. (为了获得我刚刚使用的id
权利table_a
min("id")
- 内连接以上两个并使用结果进行更新
table_b
。
解决方案
这是使用公用表表达式的一种选择:
with
ta as (
select ta.*, min(id) over(partition by customer_id, product_id) min_id
from table_a ta
),
upd as (
update table_b tb
set table_a_id = ta.min_id
from ta
where tb.table_a_id = ta.id and ta.id <> ta.min_id
)
delete from table_a ta1
using ta
where
ta1.customer_id = ta.customer_id
and ta1.product_id = ta.product_id
and ta1.id > ta.id
第一个 CTE 将目标id
与table_a
. 然后,我们使用该信息来更新table_b
. 最后,我们删除 中的重复行table_a
,只保留最早的id
。
推荐阅读
- faunadb - 动物区系按参考搜索
- scala - 如何在Scala中映射函数的输入?
- android - 来自 FireStone 的 TextView 中的消息
- intellij-idea - 在 IntelliJ 中,使用光标前的表达式作为参数调用函数的快捷方式是什么
- python - 在 Windows 10 中使用 python 在文件中输出 Git 命令
- github - 如何为特定用户创建的标记问题创建 Github 操作?
- amazon-web-services - AWS SQS 队列中可以存储的最大消息量是多少?
- c# - 这是我得到 System.Data.SqlClient.SqlException 的错误:'Table' 附近的语法不正确。'。有人可以帮忙
- linux - Heroku 部署和 pyodbc
- ios - SwiftUI 如何构建多个导航链接