Delete Duplicate rows: There are 3 cols. Is the below query correct to delete the…
- User
Delete Duplicate rows:
There are 3 cols. Is the below query correct to delete the duplicate records?
Delete from (select * , row_number() over(partition by col1, col2, col3 order by col3 ) "rn" from table_name)
tab1where tab1.rn > 1 ; - Assistant
Unfortunately, you cannot directly delete from a select statement or a derived table. However, you can use a Common Table Expression (CTE) or a temporary table to achieve this. Here's how you can do this using a CTE:
sqlWITH CTE as ( SELECT col1, col2, col3, row_number() over(PARTITION BY col1, col2, col3 ORDER BY col3) as rn FROM table_name ) DELETE FROM CTE WHERE rn > 1;Please note that using
row_number()and deleting rows with row number > 1 will only keep one row in the event of duplicates. If your requirement is different, you may need to adjust the query accordingly.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.