我正在尝试根据子操作更新父操作。如果所有子记录都是“D”操作,请将父操作设置为“D”。否则,保持父操作不变。 这是我到目前为止的查询。我想帮助摆脱硬编码的值:
UPDATE tableA
SET parentOperation = CASE
WHEN (
SELECT DISTINCT childOperation
FROM (
SELECT text_number
,text_type
,childOperation
FROM tableA
WHERE id = '1'
AND VERSION = 100
)
) = 'D'
THEN 'D'
ELSE parentOperation
END
WHERE id = '1'
AND version = 100;
示例数据:
calculated_id (primary key) id version parentOperation text_number text_type childOperation
1_10 1 100 U 10 aa D
1_20 1 100 U 20 bb D
2_10 2 100 U 10 aa D
2_20 2 100 U 20 bb U
预期产出
calculated_id (primary key) id version parentOperation text_number text_type childOperation
1_10 1 100 D 10 aa D
1_20 1 100 D 20 bb D
2_10 2 100 U 10 aa D
2_20 2 100 U 20 bb U
答案 0 :(得分:1)
尝试:
UPDATE tableA a
SET parentOperation = 'D'
WHERE NOT EXISTS(
SELECT null FROM tableA b
WHERE (a.id, a.VERSION) = (b.id, b.VERSION)
AND b.childOperation <> 'D'
)
WHERE id = '1'
AND version = 100;
答案 1 :(得分:0)
试试这个:
UPDATE tableA p
SET parentOperation = childOperation
WHERE 1 = ( SELECT COUNT (DISTINCT childOperation)
FROM tableA c
WHERE c.id = p.id
AND c.version = p.version
GROUP BY id, version
HAVING COUNT (DISTINCT childOperation) = 1);