Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rob41
Yak Posting Veteran
67 Posts |
Posted - 2011-02-10 : 12:17:44
|
| How do I delete duplicate records in a table, I want to delete all duplicate records that have a commitment history = nullexampleRecord no shipment no commitment history price1234 s001 true 9991234 s001 null 1111234 s001 null 888888 s333 null 500888 s333 true 250888 s333 null 119results should be as below when query is executedRecord no shipment no commitment history price1234 s001 true 999888 s333 true 250 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2011-02-10 : 12:29:09
|
| DELETE AFROM myTable AWHERE [Commitment History] IS NULLAND EXISTS(SELECT * FROM myTable WHERE [Commitment History] IS NOT NULL AND [Record No]=A.[Record No] AND [Shipment No]=A.[Shipment No]) |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-02-10 : 12:58:50
|
| DELETE AFROM #tab AWHERE commitmenthistory IS NULLAND EXISTS(SELECT Recordno FROM #tab WHERE commitmenthistory IS NULL AND Recordno=A.Recordno AND shipmentno=A.shipmentnogroup by Recordno having COUNT(*)>1) |
 |
|
|
rob41
Yak Posting Veteran
67 Posts |
Posted - 2011-02-10 : 17:07:36
|
[quote]Originally posted by robvolk DELETE AFROM myTable AWHERE [Commitment History] IS NULLAND EXISTS(SELECT * FROM myTable WHERE [Commitment History] IS NOT NULL AND [Record No]=A.[Record No] AND [Shipment No]=A.[Shipment No])[/quoteI just want it to delete all duplicates from parts databaseI was trying to get something like this to runDELETE FROM [parts] WHERE CommitHistory IS NULL AND [Row] > 1row being > 1 should qualify as a duplicate record |
 |
|
|
Ranjit.ileni
Posting Yak Master
183 Posts |
Posted - 2011-02-10 : 23:25:54
|
| create table #temp(Recordno int,shipmentno varchar(10),commitmenthistory varchar(10),price float)goinsert into #temp values(1234,'s001','true',999),(1234,'s001',NULL,111),(1234,'s001',NULL,888),(888,'s333',NULL,500),(888,'s333','true',250),(888,'s333',NULL,119)goselect * from #tempgo-- Define a CTE with the name "dt" ;WITH dt AS ( SELECT ROW_NUMBER() OVER ( PARTITION BY Recordno,shipmentno ORDER BY Recordno ) AS 'RowNumber', * FROM #temp where CommitmentHistory is not NULL)-- and select the data from the CTESELECT Recordno,shipmentno,commitmenthistory,priceFROM dtWHERE RowNumber =1--Ranjit |
 |
|
|
|
|
|
|
|