【问题标题】:How to update the column when the condition is true using CASE当条件为真时如何使用 CASE 更新列
【发布时间】:2019-12-24 06:05:25
【问题描述】:

我有一张表LOADING_ZONE 有列


aid, a_name, addrid, addr1, addr2, city, pstate, country, postalcd, contactnumber, attendancekey, 
attendancedate, attendedyesno

和另一个表STAGE_TABLE 与列


aid, a_name, addrid, addr1, addr2, city, pstate, country, postalcd, contactnumber, attendancekey, 
attendancedate, attendedyesno,action_indicator 

我已使用以下方法将 loading_zone 中的数据插入到 stage_table 中:

INSERT INTO stage_table(aid, a_name, addrid, addr1, addr2, city, pstate, country, postalcd, 
contactnumber, attendancekey, attendancedate, attendedyesno)
 

SELECT aid, a_name, addrid, addr1, addr2, city, pstate, country, postalcd, contactnumber, attendancekey, attendancedate, attendedyesno

FROM loading_zone

当两个表中存在相同的辅助时,我想将 action_indicator 设为“U”,否则为“I”

我试过了,但收到一个错误:

assign="update stage_table set action_indicator = (CASE when loading_zone.aid=stage_table.aid then 'U' else 'I' end)"

错误是:

psycopg2.errors.UndefinedTable:缺少表“loading_zone”的 FROM 子句条目

第 1 行:...ate stage_table set action_indicator = (CASE when loading_zone...

【问题讨论】:

  • 在此处查看接受的答案:stackoverflow.com/questions/7869592/…
  • 当您将INSERT 转换为stage_table 时,没有现有的行,因此没有现有的辅助工具可供比较。因此,由于您是从loading_zoneINSERTaid,在完成INSERT 之后,aid 对于两个表将是相同的,所以您不妨硬编码action_indicator='U' --如果这种理解不正确,请澄清您的问题。此外,如果您正在寻找UPDATE,您能否指出它们的关系(即,哪个列是主键,是否涉及外键?)
  • 是的,这就是我想要做的,但是如果我创建辅助作为主键,我会收到重复错误,所以为了避免它我做了一个额外的列 stg作为主键,数据类型为串行
  • update stage_table set action_indicator = CASE WHEN loading_zone.aid=stage_table.aid THEN 'U' ELSE 'I' END from loading_zone 试试这个。
  • 是的,谢谢它在很长一段时间后起作用了@BhartiMohane

标签: python sql postgresql


【解决方案1】:

我针对 STAGE_TABLE 和 LOADING_ZONE 创建了两个示例表(test 和 test1),以在我的末尾复制用例。以下更新查询将为您提供预期的输出。

create table test (aid integer, action_indicator character varying(1));
create table test1 (aid integer);

insert into test(aid) values(1);
insert into test(aid) values(2);
insert into test(aid) values(3);
insert into test(aid) values(4);

insert into test1(aid) values(1);
insert into test1(aid) values(2);
insert into test1(aid) values(3);


update test set action_indicator=a.action_indicator from (select aid,case when test.aid in(select aid from test1) then 'U' else 'I' end as action_indicator 
from test)a where a.aid=test.aid

select * from test

输出

1   "U"
2   "U"
3   "U"
4   "I"

【讨论】:

    【解决方案2】:

    你可以使用update:

    update stage_table st
        set action_indicator =
            (case when exists (select 1
                               from loading_zone lz
                               where lz.aid = st.aid
                              )
                  then 'U' else 'I'
             end);
    

    如果您将所有指标初始化为'I'(使用UPDATEDEFAULT),那么您可以在插入数据时执行此操作:

    INSERT INTO stage_table (aid, a_name
                             addrid, addr1, addr2, city, pstate, country, postalcd,
                             contactnumber, attendancekey, attendancedate, attendedyesno,
                             action_indicator
                            )
        SELECT aid, a_name,
               addrid, addr1, addr2, city, pstate, country, postalcd,
               contactnumber, attendancekey, attendancedate, attendedyesno,
               'U'
        FROM loading_zone;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多