sql - Cannot update the view or function 'cte' because it contains aggregates, or a DISTINCT or GROUP BY clause, or PIVOT or UNPIVOT operator -
i want delete using having .
so try execute following statement :
;with cte ( select emp_num, [from_date],[to_date],[ req_ser], [ req_year] empmission group emp_num, [from_date],[to_date],[ req_ser], [ req_year] having count(*) >2 ) delete cte
but following exception :
cannot update view or function 'cte' because contains aggregates, or distinct or group clause, or pivot or unpivot operator.
use window functions:
with todelete ( select em.*, row_number() on (partition emp_num, [from_date],[to_date],[ req_ser], [ req_year] order (select null)) cnt empmission em ) delete todelete cnt > 2;
note deletes all rows duplicate values. often, want keep 1 of values. if case, ask question.
Comments
Post a Comment