Postgres 使用多列子查询对行进行批量更新
我需要使用父表上 6 个字段的子表中的汇总值(计数、最小值、最大值)更新表(父表)中的所有行
tab1 (parent table)
-------------------
tab1_ID
tab1_low
tab1_high
tab2_ref_count
tab2_ref_low
tab2_ref_high
tab3_ref_count
tab3_ref_low
tab3_ref_high
STUS_CD
tab2 (link table for tab1 to tab1)
----------------------------------
tab1_ID
tab1_ref_ID
tab3 (link table for tab1 to tab4)
----------------------------------
tab1_ID
tab4_ref_ID
tab4
-----
tab4_ID
tab4_low
tab4_high
要将 tab2 和 tab3 的计数汇总到 tab1 中,下面是查询我我正在尝试 -
UPDATE tab1
SET (tab2_ref_count, tab2_ref_low, tab2_ref_high) =
(SELECT COUNT(t1.tab1_ID), MIN(t1.tab1_low), MAX(t1.tab1_high)
FROM tab2 t2 JOIN tab1 t1 ON (t2.tab1_ref_ID = t1.tab1_ID)
WHERE tab1.tab1_ID = t2.tab1_ID),
(tab3_ref_count, tab3_ref_low, tab3_ref_high) =
(SELECT COUNT(t4.tab4_ID), MIN(t4.tab4_low), MAX(t4.tab4_high)
FROM tab3 t3 JOIN tab4 t4 ON (t3.tab4_ref_ID = t4.tab4_ID)
WHERE tab1.tab1_ID = t3.tab1_ID)
WHERE STUS_CD IN ('01','02')
但显然它不起作用。有什么建议吗?
I need to update all the rows in a table(parent table) with summary values (count, min, max) from child tables on 6 fields on the parent table
tab1 (parent table)
-------------------
tab1_ID
tab1_low
tab1_high
tab2_ref_count
tab2_ref_low
tab2_ref_high
tab3_ref_count
tab3_ref_low
tab3_ref_high
STUS_CD
tab2 (link table for tab1 to tab1)
----------------------------------
tab1_ID
tab1_ref_ID
tab3 (link table for tab1 to tab4)
----------------------------------
tab1_ID
tab4_ref_ID
tab4
-----
tab4_ID
tab4_low
tab4_high
To roll up counts of tab2 and tab3 into tab1, below is the query I am attempting -
UPDATE tab1
SET (tab2_ref_count, tab2_ref_low, tab2_ref_high) =
(SELECT COUNT(t1.tab1_ID), MIN(t1.tab1_low), MAX(t1.tab1_high)
FROM tab2 t2 JOIN tab1 t1 ON (t2.tab1_ref_ID = t1.tab1_ID)
WHERE tab1.tab1_ID = t2.tab1_ID),
(tab3_ref_count, tab3_ref_low, tab3_ref_high) =
(SELECT COUNT(t4.tab4_ID), MIN(t4.tab4_low), MAX(t4.tab4_high)
FROM tab3 t3 JOIN tab4 t4 ON (t3.tab4_ref_ID = t4.tab4_ID)
WHERE tab1.tab1_ID = t3.tab1_ID)
WHERE STUS_CD IN ('01','02')
But apparently it's not working. Any tips please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可以这样工作:
要点:
我使用
count(*)
而不是count(t4.tab1_id)
。这里保证了相同的结果,但更简单、更快。该查询假设
tab3
中也存在与tab2
中的每个tab1_id
对应的行。如果情况并非如此,您必须将JOIN
类型更改为LEFT JOIN
或FULL OUTER JOIN
。在这种情况下,请在最终 WHERE 子句中使用提供的替代方案。如果在
tab2
或tab3
中未找到相关行,则此查询根本不会更新tab1
中的行。Could work like this:
Major points:
I use
count(*)
instead ofcount(t4.tab1_id)
. Same result guaranteed here, but simpler, faster.The query assumes that there are rows in
tab3
for everytab1_id
that is intab2
, too. If that is not the case, you have to change theJOIN
type toLEFT JOIN
orFULL OUTER JOIN
. Use the provided alternative in the final WHERE clause in this case.This query will not update rows in
tab1
at all, if not related rows are found in eithertab2
ortab3
.PostgreSQL 不支持更新语句中的元组定义(不过我认为这是计划好的)
您可以使用类似这样的内容(未测试):(
其中
...
表示第二个子的第二个派生表在您的示例中选择)PostgreSQL does not support tuple definitions in an update statement (I think it's planned though)
You can use something like this (not tested):
(where
...
indicates a second derived table for the second sub-select in your example)