我已经在这 5 个用 postgresql 编写并工作的 sql 脚本上苦苦挣扎了好几天。 我必须使用 Oracle 方言进行转换,我知道“update...from”语法不起作用,所以我正在尝试修复它。
我也经常收到错误 ORA-00907:缺少右括号。
我不知道如何转换它...请帮助我
-- Step 1: Aggiornare i valori delle righe duplicate principali
UPDATE cat_visit_step
SET accordion_closed = subquery.accordion_closed,
accordion_enabled = subquery.accordion_enabled
FROM (
SELECT etrv1.ref_next_exam_type_visit_step AS id1,
(vs1.accordion_closed OR vs2.accordion_closed) AS accordion_closed,
(vs1.accordion_enabled OR vs2.accordion_enabled) AS accordion_enabled
FROM cat_exam_type_ref_visit_step etrv1
JOIN cat_exam_type_ref_visit_step etrv2
ON etrv1.ref_exam_type_visit_step = etrv2.ref_exam_type_visit_step
AND etrv1.ref_next_exam_type_visit_step < etrv2.ref_next_exam_type_visit_step
JOIN cat_visit_step vs1 ON etrv1.ref_next_exam_type_visit_step = vs1.id
JOIN cat_visit_step vs2 ON etrv2.ref_next_exam_type_visit_step = vs2.id
WHERE vs1.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
AND vs2.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
) AS subquery
WHERE cat_visit_step.id = subquery.id1;
-- Step 2: Aggiornare le referenze duplicate
-- Recuperiamo le coppie duplicate e gli id da aggiornare e cancellare
UPDATE cat_visit_step
SET accordion_closed = subquery.accordion_closed,
accordion_enabled = subquery.accordion_enabled
FROM (
SELECT vs1.id AS id1,
(vs1.accordion_closed OR vs2.accordion_closed) AS accordion_closed,
(vs1.accordion_enabled OR vs2.accordion_enabled) AS accordion_enabled
FROM (
SELECT etrv1.ref_exam_type_visit_step, etrv1.ref_next_exam_type_visit_step AS id1, etrv2.ref_next_exam_type_visit_step AS id2
FROM cat_exam_type_ref_visit_step etrv1
JOIN cat_exam_type_ref_visit_step etrv2
ON etrv1.ref_exam_type_visit_step = etrv2.ref_exam_type_visit_step
AND etrv1.ref_next_exam_type_visit_step < etrv2.ref_next_exam_type_visit_step
JOIN cat_visit_step vs1 ON etrv1.ref_next_exam_type_visit_step = vs1.id
JOIN cat_visit_step vs2 ON etrv2.ref_next_exam_type_visit_step = vs2.id
WHERE vs1.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
AND vs2.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
) AS temp_ids
JOIN cat_visit_step_ref vsr1 ON temp_ids.id1 = vsr1.ref_visit_step
JOIN cat_visit_step vs1 ON vsr1.ref_next_visit_step = vs1.id
JOIN cat_visit_step_ref vsr2 ON temp_ids.id2 = vsr2.ref_visit_step
JOIN cat_visit_step vs2 ON vsr2.ref_next_visit_step = vs2.id
WHERE vs1.name = vs2.name
) AS subquery
WHERE cat_visit_step.id = subquery.id1;
-- Step 3: Eliminare le righe duplicate da cat_visit_step_ref
DELETE FROM cat_visit_step_ref
WHERE ref_next_visit_step IN (
SELECT vsr2.ref_next_visit_step
FROM cat_visit_step_ref vsr1
JOIN cat_visit_step_ref vsr2
ON vsr1.ref_visit_step = vsr2.ref_visit_step
AND vsr1.ref_next_visit_step < vsr2.ref_next_visit_step
JOIN cat_visit_step vs1 ON vsr1.ref_next_visit_step = vs1.id
JOIN cat_visit_step vs2 ON vsr2.ref_next_visit_step = vs2.id
WHERE vs1.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
AND vs2.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
);
-- Step 4: Eliminare le righe duplicate da cat_exam_type_ref_visit_step
DELETE FROM cat_exam_type_ref_visit_step
WHERE ref_next_exam_type_visit_step IN (
SELECT etrv2.ref_next_exam_type_visit_step
FROM cat_exam_type_ref_visit_step etrv1
JOIN cat_exam_type_ref_visit_step etrv2
ON etrv1.ref_exam_type_visit_step = etrv2.ref_exam_type_visit_step
AND etrv1.ref_next_exam_type_visit_step < etrv2.ref_next_exam_type_visit_step
JOIN cat_visit_step vs1 ON etrv1.ref_next_exam_type_visit_step = vs1.id
JOIN cat_visit_step vs2 ON etrv2.ref_next_exam_type_visit_step = vs2.id
WHERE vs1.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
AND vs2.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
);
-- Step 5: Eliminare le righe duplicate da cat_visit_step
DELETE FROM cat_visit_step
WHERE id IN (
SELECT etrv2.ref_next_exam_type_visit_step
FROM cat_exam_type_ref_visit_step etrv1
JOIN cat_exam_type_ref_visit_step etrv2
ON etrv1.ref_exam_type_visit_step = etrv2.ref_exam_type_visit_step
AND etrv1.ref_next_exam_type_visit_step < etrv2.ref_next_exam_type_visit_step
JOIN cat_visit_step vs1 ON etrv1.ref_next_exam_type_visit_step = vs1.id
JOIN cat_visit_step vs2 ON etrv2.ref_next_exam_type_visit_step = vs2.id
WHERE vs1.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
AND vs2.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
);
我需要相同的精确结果和逻辑
vs1.accordion_closed OR vs2.accordion_closed
不是有效的语法。假设您的列具有值 'Y'
和 'N'
那么您可以使用
CASE WHEN vs1.accordion_closed = 'Y' OR vs2.accordion_closed = 'Y' THEN 'Y' ELSE 'N' END
UPDATE ... SET ... FROM ...
不是有效的 Oracle 语法。而是使用 UPDATE ... SET ... WHERE ...
和相关子查询。类似:
UPDATE cat_visit_step
SET accordion_closed = subquery.accordion_closed,
accordion_enabled = subquery.accordion_enabled
WHERE cat_visit_step.id IN (
SELECT etrv1.ref_next_exam_type_visit_step AS id1,
CASE WHEN vs1.accordion_closed = 'Y' OR vs2.accordion_closed = 'Y' THEN 'Y' ELSE 'N' END AS accordion_closed,
CASE WHEN vs1.accordion_enabled = 'Y' OR vs2.accordion_enabled = 'Y' THEN 'Y' ELSE 'N' END AS accordion_enabled
FROM cat_exam_type_ref_visit_step etrv1
JOIN cat_exam_type_ref_visit_step etrv2
ON etrv1.ref_exam_type_visit_step = etrv2.ref_exam_type_visit_step
AND etrv1.ref_next_exam_type_visit_step < etrv2.ref_next_exam_type_visit_step
JOIN cat_visit_step vs1
ON etrv1.ref_next_exam_type_visit_step = vs1.id
JOIN cat_visit_step vs2
ON etrv2.ref_next_exam_type_visit_step = vs2.id
WHERE vs1.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
AND vs2.name = 'OBJECTIVE_EXAM_AND_ANAMNESIS'
);
(注意:未经测试,因为我无权访问您的表格或数据来进行测试。)
您可以以类似的方式修复第二个查询,并且
DELETE
查询在语法上似乎是有效的。