Redshift Error when executing the delete script with EXISTS function. The Select runs fine for this query

huangapple go评论55阅读模式
英文:

Redshift Error when executing the delete script with EXISTS function. The Select runs fine for this query

问题

这个Redshift查询失败 -

DELETE FROM TBL_1 stg
WHERE EXISTS (
WITH CCDA as (
SELECT
row_number() OVER (PARTITION BY emp_id,customer_id ORDER BY seq_num desc) rn
, *
FROM TBL_2
WHERE end_dt > (SELECT max(end_dt) FROM TBL_3)
)
SELECT emp_id,customer_id FROM CCDA WHERE rn = 1
AND stg.emp_id = CCDA.emp_id
AND stg.customer_id = CCDA.customer_id
);

错误:无效操作:语法错误在或靠近 "stg"。

然而,下面的查询运行正常 -

SELECT * FROM TBL_1 stg
WHERE EXISTS (
WITH CCDA as (
SELECT
row_number() OVER (PARTITION BY emp_id,customer_id ORDER BY seq_num desc) rn
, *
FROM TBL_2
WHERE end_dt > (SELECT max(end_dt) FROM TBL_3)
)
SELECT emp_id,customer_id FROM CCDA WHERE rn = 1
AND stg.emp_id = CCDA.emp_id
AND stg.customer_id = CCDA.customer_id
);

我是否遗漏了什么?

英文:

This Redshift query fails -

DELETE FROM TBL_1 stg
WHERE EXISTS (
WITH CCDA as (
SELECT
row_number() OVER (PARTITION BY emp_id,customer_id ORDER BY seq_num desc) rn
, *
FROM TBL_2
WHERE end_dt > (SELECT max(end_dt) FROM TBL_3)
)
SELECT emp_id,customer_id FROM CCDA WHERE rn = 1
AND stg.emp_id = CCDA.emp_id
AND stg.customer_id = CCDA.customer_id
);

Error: Invalid operation: syntax error at or near "stg"


However, the below query runs fine -

SELECT * FROM TBL_1 stg
WHERE EXISTS (
WITH CCDA as (
SELECT
row_number() OVER (PARTITION BY emp_id,customer_id ORDER BY seq_num desc) rn
, *
FROM TBL_2
WHERE end_dt > (SELECT max(end_dt) FROM TBL_3)
)
SELECT emp_id,customer_id FROM CCDA WHERE rn = 1
AND stg.emp_id = CCDA.emp_id
AND stg.customer_id = CCDA.customer_id
);

Am I missing something?

答案1

得分: 1

不能在DELETE语句中针对目标表使用别名。 "stg" 不能作为别名使用,这就是您收到此错误的原因。

此外,要在DELETE语句中引用其他表,您需要使用USING子句。

查看:https://docs.aws.amazon.com/redshift/latest/dg/r_DELETE.html

一个快速的示例(未经测试):

WITH CCDA as (
  SELECT
    row_number() OVER (PARTITION BY emp_id,customer_id ORDER BY seq_num desc) rn
  , *
  FROM TBL_2
  WHERE end_dt > (SELECT max(end_dt) FROM TBL_3)
)
DELETE FROM TBL_1
USING CCDA
WHERE CCDA.rn = 1
AND TBL_1.emp_id = CCDA.emp_id
AND TBL_1.customer_id = CCDA.customer_id
;
英文:

You cannot use an alias in a DELETE statement for the target table. "stg" cannot be used as the alias and this is why you are getting this error.

Also to reference other tables in a DELETE statement you need to use the USING clause.

See: https://docs.aws.amazon.com/redshift/latest/dg/r_DELETE.html

A quick stab of what this would look like (untested):

WITH CCDA as (
  SELECT
    row_number() OVER (PARTITION BY emp_id,customer_id ORDER BY seq_num desc) rn
  , *
  FROM TBL_2
  WHERE end_dt > (SELECT max(end_dt) FROM TBL_3)
)
DELETE FROM TBL_1
USING CCDA
WHERE CCDA.rn = 1
AND TBL_1.emp_id = CCDA.emp_id
AND TBL_1.customer_id = CCDA.customer_id
;

huangapple
  • 本文由 发表于 2023年2月19日 12:29:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定