I want to delete min Id if there is multiple employeeId. How can i do that in sQL query? Attacted my SQL query result
delete @CompPayDetEE where exists (select 1 from @CompPayDetEE group by AppEmployeeId )
I want to delete min Id if there is multiple employeeId. How can i do that in sQL query? Attacted my SQL query result
delete @CompPayDetEE where exists (select 1 from @CompPayDetEE group by AppEmployeeId )
This should work. I am retaining all max ids and deleting the remaining rows:
DELETE FROM Yourtable
WHERE id NOT IN
(
SELECT MAX(id)
FROM Yourtable
GROUP BY AppEmployeeID
);
declare @Table table(ID int, EmployID varchar(10), TaxAmount int, TaxableAmount int)
insert into @Table values
(1, '1', 1, 0)
,(2, '1', 13, 1)
,(3, '3', 52, 4)
WITH cte AS(
SELECT *,
ROW_NUMBER() OVER(PARTITION BY EmployID ORDER BY id desc) as RN
FROM @Table
)
DELETE cte
WHERE RN <> 1;
SELECT * from @Table;
delete @CompPayDetEE
where Id in( select IdToDelete = min(Id)
from @CompPayDetEE
group by AppEmployeeId
having count(*)>1)