This will let you find all rows that contain duplicate records for certain column(s)<br>(helpfull if you try to build a pk on a table which already contains data).<br><br>select count(*), col1, col2... coln<br>from table_name<br>group by col1, col2... coln<br>having count(*) > 1<br><br>This will let you get rid of duplicate rows in a table.<br>MAKE A BACKUP FIRST <br><br>--store all distinct records in a separate table<br>select distinct *<br>into #temp_table<br>from table_name<br><br>--empty the original table<br>truncate table_name<br><br>-- reinsert records from #temp_table back to the original <br>--one<br>insert table_name<br>select * from #temp_table<br><br>Hopefully that will help.<br>
I need to delete a large amount of duplicate rows from a table before my managment team discover the sales figures are not as good as the report tells them..
I have tried using the code below but it does not seem to work
select distinct *
into #temp_table
from table_name
If I then query the #temp_table it still has the duplicate rows.
select
field1,
field2,
...... ,
max(smalldatetime1), /* or min */
max(smalldatetime2) /* or min */
into
#temptable
from
tablename
group by field1, field2, ..... /* not smalldatetime fields */
This should give you the distinct records (if it was the smalldatetime fields causing the differences) that you need.
Try putting a column name for the two calculated
fields as SQL server does not know what to call them in the
#temptable:
select
field1,
field2,
...... ,
max(smalldatetime1) as Maxdate1, /* or min */
max(smalldatetime2) as Maxdate2 /* or min */
into
#temptable
from
tablename
group by field1, field2, .....
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.