【问题标题】:Update values in a column based on a comparison of two other tables根据其他两个表的比较更新列中的值
【发布时间】:2017-03-24 20:35:16
【问题描述】:

在 postgres 中,我有 3 个由两个键连接的表:

键A连接表1和表2,在表1中不唯一,在表2中唯一,在表3中不存在

Key B 连接表 2 和表 3,表 1 中不存在,在表 2 和表 3 中是唯一的。


我希望更新表 3 中的一列,该列应包含表 1 中的记录计数(该列中的所有值都是 0)。

表_1

+-----+
| Key |
+-----+
| A1  |
| A1  |
| A1  |
| A2  |
| A2  |
| A3  |
| A3  |
+-----+

表_2

+-------+-------+
| Key_A | Key_B |
+-------+-------+
| A1    | B1    |
| A2    | B2    |
| A3    | B3    |
+-------+-------+

Table_3(期望的结果)

+-------+--------+
| Key_B | Count  |
+-------+--------+
| B1    |      3 |
| B2    |      2 |
| B3    |      2 |
+-------+--------+

我被更新命令卡住了(对它们还不够熟悉),我有我需要的计数:

Select Table_3.Key_B, count(Table_1.*)
from Table_1
Join Table_2 on Table_1.Key_A = Table_2.Key_A
Join Table_3 on Table_2.Key_B = Table 3.Key_B
Group by 1

我只是不太确定如何使用正确的计数更新表 3 中的记录。我想我可能需要一个功能,但我不确定。这是在正确的轨道上吗?

Create or replace function my_funct
  returns varchar as
$body$
Declare
      r         record
begin
      select key_B from Table_3 into r;

      update Table_3
      set count = ( 
                   select count(*) 
                   from table_1
                   Join Table_2 on Table_1.Key_A = Table_2.Key_A
                   Join Table_3 on Table_2.Key_B = Table 3.Key_B
                   Where key_B = r
                   );  
end
$body$

【问题讨论】:

    标签: sql postgresql sql-function


    【解决方案1】:

    子查询中需要一个相关条件:

     update Table_3
          set count = (select count(*) 
                       from table_1 t1 join
                            table_2 t2
                            on t1.Key_A = t2.Key_A
                       where t2.Key_B = Table_3.Key_B
                      ); 
    

    Table_3 的引用来自外部查询。

    实际上,假设Table_1Table_2Table_1 之间存在适当的外键关系,您甚至不需要join 没有重复项:

     update Table_3
          set count = (select count(*) 
                       from table_2 t2
                       where t2.Key_B = Table_3.Key_B
                      ); 
    

    查看问题后,我意识到情况并非如此,但我还是提供了解决方案。

    【讨论】:

      【解决方案2】:

      避免昂贵的相关子查询:

      update t3
      set c = t.c
      from (
          select t2.b, count(*) as c
          from t1 join t2 on t1.a = t2.a
          group by 1
      ) t
      where t3.b = t.b
      ;
      table t3;
       b | c 
      ---+---
       1 | 3
       2 | 2
       3 | 2
      

      据我了解,table_2 中的key_Btable_3 的外键,因此无需在from 子句中加入table_3

      create table t2 (a int primary key, b int unique);
      create table t1 (a int references t2);
      create table t3 (b int unique references t2, c int);
      
      insert into t2 (a, b) values (1,1),(2,2),(3,3);
      insert into t1 (a) values (1),(1),(1),(2),(2),(3),(3);
      insert into t3 (b) values (1),(2),(3);
      

      【讨论】:

        【解决方案3】:
        UPDATE #table3 SET _Count = Cnt FROM (SELECT COUNT(*) Cnt,#table1.Key_A,Key_B  KeyB FROM #table1 JOIN #table2 ON #table1.Key_A = #table2.Key_A GROUP BY #table1.Key_A,Key_B ) A WHERE KeyB = Key_B
        

        【讨论】:

          猜你喜欢
          • 2021-01-28
          • 2018-10-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多