0

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 )

SQL Result

Jaydip Jadhav
  • 12,179
  • 6
  • 24
  • 40
RDeveloper
  • 121
  • 2
  • 12

3 Answers3

0

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
         );
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Teja
  • 13,214
  • 36
  • 93
  • 155
0
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;
Ahmed Saeed
  • 831
  • 7
  • 12
0
delete @CompPayDetEE
where Id in(    select IdToDelete = min(Id)
                from @CompPayDetEE
                group by AppEmployeeId
                having count(*)>1)
Marta B
  • 438
  • 2
  • 9