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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Help required with Delete logic

Author  Topic 

clinton_eg
Yak Posting Veteran

60 Posts

Posted - 2013-05-13 : 09:31:25
Hi

I have data in the following format. And data get inserted into the table on a daily basis.

Name |Age |City |DOB |InsertDate
Tom |21 |Austin |4/30/2000 |5/1/2013
Brian |30 |Sydney |6/2/1985 |5/2/2013
Brian | |Sydney | |5/3/2013
Brian | |Sydney | |5/4/2013
Brian | |Sydney | |5/5/2013
Charlie | |New York | |5/10/2013
Charlie | |New York | |5/11/2013
Charlie | |New York | |5/12/2013

Scenario 1: The entry for Brian, in this case there is one complete record and the other entries which got added on 3rd, 4th and 5th May do not have the Age and DOB.
Requirement: I want to delete all entries in the table except for the 2nd May record.

Scenario 2: The entries for Charlie are all the same for records inserted 10th to 12th May.
Requirement: I want to delete all records of Charlie except for the record with the min insert date (10th May 2013)

Could you advise how do I achieve this with a single delete statement, considering that the combination of Name,City & InsertDate is the PK columns.

Thanks

Ewan Gilby

stepson
Aged Yak Warrior

545 Posts

Posted - 2013-05-13 : 10:18:59
Hi,

;with P
as
(
select *,
ROW_NUMBER() OVER (partition by Name order by Age desc ,DOB desc , INsertDate asc) as rw
from Person)


delete
from P
where P.rw<>1
and P.Age is null
and P.DOB is null


Ce-am pe mine am si-n dulap, cand ma-mbrac zici ca ma mut
sabinWeb
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-14 : 00:36:58
Can there be a case where there multiple records with Age and DOB fields having non empty values?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

clinton_eg
Yak Posting Veteran

60 Posts

Posted - 2013-05-14 : 05:39:02
visakh16 - yes there can be multiple rows with Age and Dob having non empty values.. what I gave is just an example, however the volumes are in millions.

stepson - Thanks for the details.

How do I avoid inserting a duplicate record, if it come in the next days data load?

Ewan Gilby
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-14 : 05:53:50
quote:
Originally posted by clinton_eg

visakh16 - yes there can be multiple rows with Age and Dob having non empty values.. what I gave is just an example, however the volumes are in millions.

stepson - Thanks for the details.

How do I avoid inserting a duplicate record, if it come in the next days data load?

Ewan Gilby


Then previous suggestion wont work fine in those cases

you need a slight tweak then
use like

DELETE t
--SELECT *
FROM
(
select ROW_NUMBER() OVER (partition by Name order by Age desc ,DOB desc , INsertDate asc) as rw
--,*
from Person
)t
WHERE rw > 1


First do select after uncommenting the commented code and once happy revert to original above suggestion

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-05-14 : 05:56:33
quote:
Originally posted by clinton_eg

visakh16 - yes there can be multiple rows with Age and Dob having non empty values.. what I gave is just an example, however the volumes are in millions.

stepson - Thanks for the details.

How do I avoid inserting a duplicate record, if it come in the next days data load?

Ewan Gilby


For that add a not exists check in your insert statement logic like

INSERT table (Name,Age,City ,...)
SELECT Name,Age,City,..
FROM sourcetable s
WHERE NOT EXISTS (SELECT 1
FROM table
WHERE Name = s.Name
AND Age = s.Age
AND DOB = s.DOB
)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -