Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 14th, 2006, 11:43 AM
Authorized User
 
Join Date: Mar 2005
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deleting duplicate records

I have a table tmPunchtimeSummary which contains a sum of employee's hours
per day. The table contains some duplicates.

Code:
CREATE TABLE [tmPunchtimeSummary] 
(
[iTmPunchTimeSummaryId] [int] IDENTITY (1, 1) NOT NULL ,
[sCalldate] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[sEmployeeId] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[dTotalHrs] [decimal](18, 4) NULL 
) ON [PRIMARY]

INSERT tmPunchtimeSummary (sCalldate, sEmployeeId, dTotalHrs)
VALUES('20060610', '1234', 4.5)

INSERT tmPunchtimeSummary (sCalldate, sEmployeeId, dTotalHrs)
VALUES('20060610', '1234', 4.5)

INSERT tmPunchtimeSummary (sCalldate, sEmployeeId, dTotalHrs)
VALUES('20060610', '2468', 8.0)

INSERT tmPunchtimeSummary (sCalldate, sEmployeeId, dTotalHrs)
VALUES('20060610', '1357', 9.0)

INSERT tmPunchtimeSummary (sCalldate, sEmployeeId, dTotalHrs)
VALUES('20060610', '2345', 8.5)

INSERT tmPunchtimeSummary (sCalldate, sEmployeeId, dTotalHrs)
VALUES('20060610', '2345', 8.5
How can I write a delete statement to only delete the duplicates which in
this case would be the 1st and 5th records?

Thanks,
Ninel


 
Old June 14th, 2006, 12:19 PM
Authorized User
 
Join Date: Mar 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Ninel!

I don't think we have any delete statement which can be applied in such cases. One thing you can do -

(1) use select * into statement and create a new table.
       select distinct * into newtable from oldtable
(2) Drop oldtable
(3) Use sp_rename for new table.

But, problem is - You will lose all Foreign keys, Rules and Defaults. So in that case, other method is to use a stored procedure.
If you need that stored procedure, mail me at - [email protected].


- Som Dutt

 
Old June 14th, 2006, 01:53 PM
Friend of Wrox
 
Join Date: Aug 2004
Posts: 385
Thanks: 0
Thanked 0 Times in 0 Posts
Default

you can fix it with a curser and only loose foreing key info (if you have any) on the rows you delete.



 
Old June 14th, 2006, 02:12 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Michael Valentine Jones wrote this for you:

select * from tmPunchtimeSummary

delete from a
from
    tmPunchtimeSummary a
    join
    (
    select
        sCalldate,
        sEmployeeId,
        dTotalHrs,
        iTmPunchTimeSummaryId = max(iTmPunchTimeSummaryId)
    from
        tmPunchtimeSummary
    group by
        sCalldate,
        sEmployeeId,
        dTotalHrs
    having
        count(*) <> 1
    ) b
    on
    a.sCalldate = b.sCalldate and
    a.sEmployeeId = b.sEmployeeId and
    a.dTotalHrs = b.dTotalHrs and
    a.iTmPunchTimeSummaryId <> b.iTmPunchTimeSummaryId

select * from tmPunchtimeSummary







Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting Duplicate Records BrianWren Pro VB Databases 1 February 22nd, 2008 01:30 PM
duplicate records vanitha SQL Server 2000 4 June 2nd, 2007 04:35 PM
duplicate records vanitha Reporting Services 2 May 31st, 2007 01:54 AM
Duplicate Records mrookey Dreamweaver (all versions) 1 April 15th, 2005 11:23 AM
deleting only 1 duplicate entry qwjunk Classic ASP Databases 3 February 2nd, 2004 11:45 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.