0

I am trying trying to delete duplicates from Redshift table

Mycode

from sqlalchemy import create_engine
# A long string that contains the necessary Postgres login information
postgres_str = f'postgresql://{redshift_username}:{redshift_password}@{redshift_address}:{redshift_port}/{redshift_dbname}'
# Create the connection
cnx = create_engine(postgres_str)


delstatmt = '''WITH CTE AS
(
SELECT *,ROW_NUMBER() OVER (PARTITION BY org_country_code,dest_country_code,postcode,zone,kg,value,carrier,version 
ORDER BY  org_country_code,dest_country_code,postcode,zone,kg,value,carrier,version ) AS RN
FROM d.axis
)

DELETE FROM d.axis transformed WHERE RN<>1'''

cnx.execute(delstatmt)

Error

ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "DELETE"

LINE 8: DELETE FROM d.axis transformed WHERE ...

What is wrong is code. Any help appreciated.

3
  • Your connection URI begins with postgresql://. Why are you using the PostgreSQL dialect when there is a Redshift dialect that you can use? Commented Jul 24, 2020 at 15:14
  • @Gord Thompson, this works for me also I am able read from the table using this connection . Issue is with delete query Commented Jul 24, 2020 at 15:38
  • 1
    Your DELETE statement does not reference the CTE (e.g., via USING) so I would expect RN to be an unknown column name. Commented Jul 24, 2020 at 17:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.