I have a table EMP in which the values will be like this
EmpId Ename Sal DeptId
11 Ram 10000 10
11 Ram 10000 10
22 Raj 20000 20
22 Raj 20000 20
33 Anil 15000 30
33 Anil 15000 30
I want to delete only duplicate Rows. After Delete I want
the output like this
EmpId Ename Sal DeptId
11 Ram 10000 10
22 Raj 20000 20
33 Anil 15000 30
Answers were Sorted based on User's Feedback
Answer / kishor kumar
This can be solved by 2 Ways
One way
Create unique index on this Table with ignore duplicate row
2nd way.
1st transfer the data to a temp table bu using Distinct
clause then truncate the EMP table and then transfor the
data from temp table to EMP Table.
select distinct (column names) in to #temp from EMP
Truncate table EMP
INSERT INTO EMP
SELECT * FROM #temp
| Is This Answer Correct ? | 9 Yes | 3 No |
Answer / soorai ganesh
Hey dude, If u use SQLSERVER 2005 This will help u.
;WITH tmpEmp AS
(
SELECT ROW_NUMBER() OVER(PARTITION BY EmpID ORDER BY EmpID)
AS RowNumber, * FROM EMP
)
DELETE FROM tmpEmp WHERE RowNumber> 1
| Is This Answer Correct ? | 6 Yes | 0 No |
Answer / siddharth
DECLARE @int as int
DECLARE @cnt as int
WHILE EXISTS (SELECt top 1 EmpId from Emp group by EmpId having count(EmpId)>1 order by EmpId)
Begin
SELECT top 1 @int=EmpId,@cnt=Count(EmpId) from Emp group by EmpId having count(EmpId)>1 order by EmpId
Delete top (@cnt-1) from Emp where EmpId = @int
End
Select EmpId,Ename,Sal,DeptId from Emp order by EmId
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / shankar
First Of All Create Table
create table EMP(empid int, ename varchar(10),sal int,
deptid int)
insert into EMP values(11, 'Ram', 10000, 10)
insert into EMP values(11, 'Ram',10000,10)
insert into EMP values(22, 'Raj', 20000, 20)
insert into EMP values(22, 'Raj', 20000, 20)
insert into EMP values(33, 'Anil', 15000, 30)
insert into EMP values(33, 'Anil', 15000, 30)
insert into EMP values(44,'bbb',11111,40)
--Now Run This query It works
Declare @empID int, @RecCount int,@Name varchar (10),@Sal
int, @deptid int
SET @RecCount = 0
Declare MyCur Cursor for
Select empid,ename Sal,DeptID,Count(ID)-1 as Records from
EMP Group BY empid,ename Sal,DeptID having Count(empid)>1
Open MyCur
Fetch FROM MyCur INTO @empID,@Name,@sal,@DeptID,@RecCount
WHILE @@FETCH_STATUS = 0
BEGIN
SET ROWCOUNT @RecCount
DELETE from EMP where empID= @empID
SET @empID= 0
SET @Name = ''
SET @Sal= 0
SET @DeptID = 0
SET @RecCount = 0
SET ROWCOUNT 0
Fetch FROM MyCur INTO @empID,@Name,@sal,@DeptID,@RecCount
END
Close MyCur
DEallocate MyCur
| Is This Answer Correct ? | 3 Yes | 0 No |
Answer / aashish lad
-- Here Temp1 is Temporary Table So it will take All records
-- From Mytable With RowNumbar column
-- We can Delete the Record from
SELECT ROW_NUMBER() OVER(PARTITION BY empid ORDER BY empid)
AS RowNumber, * into #temp1 FROM mytable
DELETE FROM #temp1 WHERE RowNumber> 1
INSERT INTO mytable
SELECT * FROM #temp
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / smitha
;with empctc(empid,ename,sal,deptid,ranking)
as
(Select empid,ename,sal,deptid,ranking=Dense_rank() over (
partition by empid,ename,sal,deptid order by NEWID() asc)
from emp
)
delete * from empctc where ranking>1
| Is This Answer Correct ? | 2 Yes | 0 No |
Answer / guest
WITH T1 AS (
SELECT ROW_NUMBER () OVER ( PARTITION BY EmpId ORDER BY
EmpId) AS RNUM,EmpId FROM EMP)
delete
FROM T1 WHERE RNUM > 1
| Is This Answer Correct ? | 1 Yes | 0 No |
I have tried this query in my PC.Very simply I was able to
remove all duplicate rows.
Please try below query it will work out.
SELECT DISTINCT*FROM EMP(TableName);
You will get below table
EmpId Ename Sal DeptId
11 Ram 10000 10
22 Raj 20000 20
33 Anil 15000 30
Suppose if you have different ENAME but you want only
selected ENAME without duplicate records.
Where We can use below query.
SELECT DISTINCT*FROM EMP WHERE ENAME IN('RAM','RAJ','ANIL');
Please correct me if I am wrong.
| Is This Answer Correct ? | 1 Yes | 1 No |
Answer / vikas kant
select Distinct * into #Temp_Temp1 from Emp
truncate table Emp
insert into Emp select * from #Temp_Temp1
select * from Emp
| Is This Answer Correct ? | 0 Yes | 0 No |
Answer / mohan
create table #temp(empid int, ename varchar(10),sal int,
deptid int)
insert into #temp values(11, 'Ram', 10000, 10)
insert into #temp values(11, 'Ram',10000,10)
insert into #temp values(22, 'Raj', 20000, 20)
insert into #temp values(22, 'Raj', 20000, 20)
insert into #temp values(33, 'Anil', 15000, 30)
insert into #temp values(33, 'Anil', 15000, 30)
insert into #temp values(44,'bbb',11111,40)
select * from #temp
set rowcount 1
delete from #temp where empid in(select empid from #temp
group by empid having count(*)>1)
while @@rowcount>0
begin
delete from #temp where empid in(select empid from #temp
group by empid having count(*)>1)
end
set rowcount 0
select * from #temp
| Is This Answer Correct ? | 0 Yes | 1 No |
Why de-normalization required?
What is SubQuery in SQL Server 2008
How to give a user the option of importing Excel and a delimited text file into a SQL Server Database without manually using SQL DTS?
what is denormalization and when would you go for it? : Sql server database administration
What are the advantages of mirroring?
What is raid? : SQL Server Architecture
How to list all columns in a table using odbc_columns()?
What are temporal tables in sql server 2016?
What are the advantages of using third-party tools?
Difference between LEN() and DATALENGTH() in sql server ?
What is the difference between truncate and delete commands?
what is cluster and nin-cluster index?
Oracle (3259)
SQL Server (4518)
MS Access (429)
MySQL (1402)
Postgre (483)
Sybase (267)
DB Architecture (141)
DB Administration (291)
DB Development (113)
SQL PLSQL (3330)
MongoDB (502)
IBM Informix (50)
Neo4j (82)
InfluxDB (0)
Apache CouchDB (44)
Firebird (5)
Database Management (1411)
Databases AllOther (288)