May 24, 2010 at 7:23 am
hi
How to delete the duplicate records in the below table without using Temp table or permanent table and/or SET Rowcount XXXXX or Cursor
Create table t1 (i int,b int)
Insert into t1
select 1,7
union all select 4,7
union all select 4,7
union all select 4,7
union all select 4,7
union all select 4,8
union all select 5,8
union all select 5,8
union all select 5,8
union all select 5,8
union all select 6,4
union all select 6,4
union all select 7,8
Select * from t1
select i,b,COUNT(*) from t1
group by i,b having COUNT(*)>1
order by i ,b
-- i need like
select 1,7
union select 4,7
union select 4,7
union select 4,7
union select 4,7
union select 4,8
union select 5,8
union select 5,8
union select 5,8
union select 5,8
union select 5,8
union select 6,4
union select 6,4
union select 7,8
Drop table t1
Thanks
Parthi
Thanks
Parthi
May 24, 2010 at 7:51 am
Try this for test purposes,
;with numbered as(SELECT rowno=row_number() over
(partition by i,b order by i),i,b from #t1)
select * from numbered
/*Giving the following results:
rowno i b
1 1 7
1 4 7
2 4 7
3 4 7
4 4 7
1 4 8
1 5 8
2 5 8
3 5 8
4 5 8
1 6 4
2 6 4
1 7 8 */
and if correct then replace the select * with :
delete * from numbered WHERE rowno > 1
May 24, 2010 at 7:53 am
This problem has been covered so many times that I'm surprised that Google doesn't give you a clue. Does it?
-- Gianluca Sartori
May 24, 2010 at 7:54 am
Since your target table seems to be identical to what you have so far, the solutionis simple: do nothing 😀
To answer your question: based on the given talbe you can't. You would need to add an identity column to differentiate the rows. Based on that you can either use ROW_NUMBER() or a CTE to get the max(ID) per i,b combination and delete the rest using a EXCEPT statement.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply