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 |
clinton_eg
Yak Posting Veteran
60 Posts |
Posted - 2013-05-13 : 09:31:25
|
HiI have data in the following format. And data get inserted into the table on a daily basis.Name |Age |City |DOB |InsertDateTom |21 |Austin |4/30/2000 |5/1/2013Brian |30 |Sydney |6/2/1985 |5/2/2013Brian | |Sydney | |5/3/2013Brian | |Sydney | |5/4/2013Brian | |Sydney | |5/5/2013Charlie | |New York | |5/10/2013Charlie | |New York | |5/11/2013Charlie | |New York | |5/12/2013Scenario 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.ThanksEwan Gilby |
|
stepson
Aged Yak Warrior
545 Posts |
Posted - 2013-05-13 : 10:18:59
|
Hi,;with Pas (select *, ROW_NUMBER() OVER (partition by Name order by Age desc ,DOB desc , INsertDate asc) as rwfrom Person)delete from Pwhere 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 mutsabinWeb |
|
|
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 MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 |
|
|
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 casesyou need a slight tweak thenuse likeDELETE t--SELECT *FROM( select ROW_NUMBER() OVER (partition by Name order by Age desc ,DOB desc , INsertDate asc) as rw--,*from Person)tWHERE rw > 1 First do select after uncommenting the commented code and once happy revert to original above suggestion------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
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 likeINSERT table (Name,Age,City ,...)SELECT Name,Age,City,..FROM sourcetable sWHERE NOT EXISTS (SELECT 1 FROM table WHERE Name = s.Name AND Age = s.Age AND DOB = s.DOB ) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|