我有一个像这样的表“列表”
Unique|S_Number|New_Attr|C_Number|C_Order|A_Number
123 101 1 -A 1 0
124 101 1 -A 1 2
125 101 1 -A 1 3
127 101 1 -B 2 4
128 101 1 -C 3 5
129 101 1 -D 4 6
我已经这样做了
Unique|S_Number|New_Attr|C_Number|C_Order|A_Number
123 101 1 -A 1 0
124 101 2 -A 1 2
125 101 3 -A 1 3
127 101 1 -B 2 4
128 101 1 -C 3 5
129 101 1 -D 4 6
仅在S_Number和C_Number的组合大于1并且A_Number大于0的情况下,才希望使用A_Number值更新New_Attr
我已经创建了临时表list_temp2以仅插入
CREATE TABLE list_temp2
(
S_number CHAR(32) NOT NULL,
C_number CHAR(32),
CNT integer
);
INSERT INTO list_temp2 p2 (p2.s_number, p2.c_number, p2.cnt)
SELECT p1.S_number, p1.C_number,COUNT(*) FROM list p1
GROUP BY p1.S_number, p1.C_number
HAVING COUNT(*)>1;
插入一行
101 -A 3
SELECT p1.s_number, p1.c_number, p1.New_Attr, p1.a_number, p2.cnt FROM list p1
LEFT JOIN
list_temp2 p2 ON p1.s_number = p2.s_number
where p1.a_number>1 AND p2.cnt>1 AND p2.s_number = p1.s_number AND p2.c_number = p1.c_number;
发出2行(预期)
101 -A 1 2 3
101 -A 1 3 3
现在我要更新列表,但如果我这样做,我将无法正确执行此操作:
UPDATE list p1 SET p1.New_Attr=
(
SELECT p1.a_number FROM list p1
LEFT JOIN
list_temp2 p2 ON p1.s_number = p2.s_number
where p1.a_number>0 AND p2.cnt>1 AND p2.s_number = p1.s_number AND p2.c_number = p1.number
);
它提供的输出值太多,因为Select命令给出了2行
答案 0 :(得分:0)
UPDATE your_table
SET New_Attr = IIF(A_Number>0 AND S_Number + C_Number > 1, A_Number, New_Attr)
要对此进行解释,请逐一查看,并根据其他行的值来正确更新结果。我可能没有正确理解更新的条件,但是如果您在iif中应用逻辑,这应该像灵符一样工作。
答案 1 :(得分:0)
我认为您想要过滤的UPDATE
:
UPDATE list
SET New_Attr = A_Number
WHERE A_Number > 0 AND S_Number + C_Number > 1;