如何避免由子查询返回的多于一行用作表达式

时间:2018-11-29 15:54:31

标签: sql postgresql join

我尝试了多种方法来更新一个有条件的表。

但是,两种方法都失败并返回错误

  

由子查询返回的多行用作表达式SQL状态:21000。

我的代码:

UPDATE "B_S1"."TC_history" AS TC
SET "CC_RESULT" = CASE
                     WHEN TT.C0 = 'b' THEN 'NEVER_CC'
                     ELSE NULL 
                  END
FROM 
    (SELECT TR."CC_RESULT" AS C0
     FROM
         (SELECT * 
          FROM "B_TRACE"."P_TC_DATA" DAT 
          WHERE DAT."STATE_ID" IN ('7','8','9')) AS DATS, 
        "B_CCN"."CCN_CONSENT_RECORD" AS SUBID, 
        "B_TRACE"."P_TC_RECORD" AS TR,
        "B_S1"."TC_history" AS TC
    WHERE (SUBID."SUBJECT_C_ID" IS NOT NULL)
        AND TR."P_SERIAL_ID"=SUBID."SUBJECT_ID"
        AND SUBID."SUBJECT_ID"=DATS."P_SERIAL_ID"
        AND TR."P_SERIAL_ID"=DATS."P_SERIAL_ID"
        AND TC."SUBJECT_C_ID"=SUBID."SUBJECT_C_ID") AS TT
WHERE (SELECT "SUBJECT_C_ID" FROM "B_S1"."TC_history")= (SELECT TC."SUBJECT_C_ID"
    FROM
        (SELECT * FROM "B_TRACE"."P_TC_DATA" DAT WHERE DAT."STATE_ID" IN ('7','8','9')) AS DATS, 
        "B_CCN"."CCN_CONSENT_RECORD" AS SUBID, 
        "B_TRACE"."P_TC_RECORD" AS TR,
        "B_S1"."TC_history" AS TC
    WHERE (SUBID."SUBJECT_C_ID" IS NOT NULL)
        AND TR."P_SERIAL_ID"=SUBID."SUBJECT_ID"
        AND SUBID."SUBJECT_ID"=DATS."P_SERIAL_ID"
        AND TR."P_SERIAL_ID"=DATS."P_SERIAL_ID"
        AND TC."SUBJECT_C_ID"=SUBID."SUBJECT_C_ID")

尝试关联方式

UPDATE 
(   SELECT *
    FROM
        (SELECT * FROM "B_TRACE"."P_TC_DATA" DAT WHERE DAT."STATE_ID" IN ('7','8','9')) AS DATS, 
        "B_CCN"."CCN_CONSENT_RECORD" AS SUBID, 
        "B_TRACE"."P_TC_RECORD" AS TR,
        "B_S1"."TC_history" AS TCH
    WHERE (SUBID."SUBJECT_C_ID" IS NOT NULL)
        AND TR."P_SERIAL_ID"=SUBID."SUBJECT_ID"
        AND SUBID."SUBJECT_ID"=DATS."P_SERIAL_ID"
        AND TR."P_SERIAL_ID"=DATS."P_SERIAL_ID"
        AND TCH."SUBJECT_C_ID"=SUBID."SUBJECT_C_ID"
        ) AS TT
SET TCH."CC_RESULT0"=CASE
WHEN TT."CC_RESULT"='b' THEN 'NEVER_CC'
ELSE NULL END;

仅当我将所有列一起插入时才起作用

INSERT INTO "B_S1".TC_history("SUBJECT_C_ID","CC_RESULT","NO_CC_RESULT",
"CC_TIME","BB_TIME_CC","BB_TIME_CCN","SITE_NAME","CC_NOTE","USER_NAME","IFTAXI","BB_TIME_TAXI","BB_LOCATION_TAXI","ESTABLISHED_TIME")
SELECT  "SUBJECT_C_ID",
CASE WHEN TR."CC_RESULT"='b' THEN 'NEVER_CC'
ELSE NULL END,
CASE WHEN TR."CC_RESULT"='b' THEN 'DEAD'
ELSE NULL END,
"CC_TIME","BB_TIME_CC","BB_TIME_CCN","SITE_NAME",TR."CC_NOTE","USER_NAME",
CASE WHEN TR."BB_LOCATION_TAXI" IS NULL THEN FALSE
ELSE TRUE END,
"BB_TIME_TAXI","BB_LOCATION_TAXI","CC_TIME"
FROM (SELECT * FROM "B_TRACE"."P_TC_DATA" DAT WHERE DAT."STATE_ID" IN ('7','8','9')) AS DATS,
"B_CCN"."CCN_CONSENT_RECORD" AS SUBID,
"B_TRACE"."P_TC_RECORD" AS TR,
"B_SYSTEM"."SYSTEM_SITE" AS SI,
"B_SYSTEM"."SYSTEM_USER" AS SU
WHERE (SUBID."SUBJECT_C_ID" IS NOT NULL)
    AND TR."P_SERIAL_ID"=SUBID."SUBJECT_ID" 
    AND SUBID."SUBJECT_ID"=DATS."P_SERIAL_ID" 
    AND TR."P_SERIAL_ID"=DATS."P_SERIAL_ID" 
    AND TR."SITE_NID"=SI."SITE_ID" 
    AND TR."USER_ID"=SU."USER_ID"

是否可以在这种情况下更新那些列?

我不想为子查询创建实际的临时表。

创建临时表是解决此问题的唯一方法吗?

0 个答案:

没有答案