Postgres 使用多列子查询对行进行批量更新

发布于 2024-12-27 09:39:47 字数 1184 浏览 1 评论 0原文

我需要使用父表上 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼睛会笑 2025-01-03 09:39:47

可以这样工作:

UPDATE tab1 t1
SET    tab2_ref_count = t2.ct
     , tab2_ref_low   = t2.low
     , tab2_ref_high  = t2.high
     , tab3_ref_count = t3.ct
     , tab3_ref_low   = t3.low
     , tab3_ref_high  = t3.high
FROM  (
    SELECT t2.tab1_id
         , count(*)          AS ct
         , min(t1.tab1_low)  AS low
         , max(t1.tab1_high) AS high
    FROM  tab2 t2
    JOIN  tab1 t1 ON t1.tab1_id = t2.tab1_ref_id 
    GROUP BY 1
    ) t2
JOIN  (
    SELECT t3.tab1_id
         , count(*)          AS ct
         , min(t4.tab1_low)  AS low
         , max(t4.tab1_high) AS high
    FROM   tab3 t3
    JOIN   tab4 t4 ON t4.tab4_id = t3.tab4_ref_id 
    GROUP  BY 1
    ) t3 USING (tab1_id)
WHERE  stus_cd IN ('01','02')
AND    t1.tab1_id = t2.tab1_id;
-- AND    t1.tab1_id = COALESCE(t2.tab1_id, t3.tab1_id);  .. for FULL OUTER JOIN

要点:

  • 我使用 count(*) 而不是 count(t4.tab1_id)。这里保证了相同的结果,但更简单、更快。

  • 该查询假设 tab3 中也存在与 tab2 中的每个 tab1_id 对应的行。如果情况并非如此,您必须将 JOIN 类型更改为 LEFT JOINFULL OUTER JOIN。在这种情况下,请在最终 WHERE 子句中使用提供的替代方案。

  • 如果在 tab2tab3 中未找到相关行,则此查询根本不会更新 tab1 中的行。

Could work like this:

UPDATE tab1 t1
SET    tab2_ref_count = t2.ct
     , tab2_ref_low   = t2.low
     , tab2_ref_high  = t2.high
     , tab3_ref_count = t3.ct
     , tab3_ref_low   = t3.low
     , tab3_ref_high  = t3.high
FROM  (
    SELECT t2.tab1_id
         , count(*)          AS ct
         , min(t1.tab1_low)  AS low
         , max(t1.tab1_high) AS high
    FROM  tab2 t2
    JOIN  tab1 t1 ON t1.tab1_id = t2.tab1_ref_id 
    GROUP BY 1
    ) t2
JOIN  (
    SELECT t3.tab1_id
         , count(*)          AS ct
         , min(t4.tab1_low)  AS low
         , max(t4.tab1_high) AS high
    FROM   tab3 t3
    JOIN   tab4 t4 ON t4.tab4_id = t3.tab4_ref_id 
    GROUP  BY 1
    ) t3 USING (tab1_id)
WHERE  stus_cd IN ('01','02')
AND    t1.tab1_id = t2.tab1_id;
-- AND    t1.tab1_id = COALESCE(t2.tab1_id, t3.tab1_id);  .. for FULL OUTER JOIN

Major points:

  • I use count(*) instead of count(t4.tab1_id). Same result guaranteed here, but simpler, faster.

  • The query assumes that there are rows in tab3 for every tab1_id that is in tab2, too. If that is not the case, you have to change the JOIN type to LEFT JOIN or FULL 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 either tab2 or tab3.

一向肩并 2025-01-03 09:39:47

PostgreSQL 不支持更新语句中的元组定义(不过我认为这是计划好的)

您可以使用类似这样的内容(未测试):(

UPDATE tab1
  SET tab2_ref_count = tt1.tab1_id_count, 
      tab2_ref_low = tt1.tab1_low_min, 
      tab2_ref_high = tt1.tab1_high_max
FROM (
     SELECT COUNT(t1.tab1_ID) as tab1_id_count, 
            MIN(t1.tab1_low) as tab1_low_min,     
            MAX(t1.tab1_high) as tab1_high_max
     FROM  tab2 t2 
     JOIN tab1 t1 ON (t2.tab1_ref_ID = t1.tab1_ID) 
     WHERE  tab1.tab1_ID = t2.tab1_ID
 ) tt1
 ....
 WHERE  STUS_CD IN ('01','02')

其中 ... 表示第二个子的第二个派生表在您的示例中选择)

PostgreSQL does not support tuple definitions in an update statement (I think it's planned though)

You can use something like this (not tested):

UPDATE tab1
  SET tab2_ref_count = tt1.tab1_id_count, 
      tab2_ref_low = tt1.tab1_low_min, 
      tab2_ref_high = tt1.tab1_high_max
FROM (
     SELECT COUNT(t1.tab1_ID) as tab1_id_count, 
            MIN(t1.tab1_low) as tab1_low_min,     
            MAX(t1.tab1_high) as tab1_high_max
     FROM  tab2 t2 
     JOIN tab1 t1 ON (t2.tab1_ref_ID = t1.tab1_ID) 
     WHERE  tab1.tab1_ID = t2.tab1_ID
 ) tt1
 ....
 WHERE  STUS_CD IN ('01','02')

(where ... indicates a second derived table for the second sub-select in your example)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文