| Author |
Topic |
|
sqlchiq
Posting Yak Master
133 Posts |
Posted - 2008-12-29 : 21:59:23
|
why doesn't this delete statement work?deletefrom deleteme2 a, (select min(date) mindate, max(date) maxdate, vin from deleteme2 group by vin)bwhere a.vin = b.vin and a.date<>b.mindate and a.date <>b.maxdateWhen its a select* statement it works.basically I have entries with 2 columns, vin and dateThere are many many many identical vins but none with the same date.I want to delete every date in between and keep only the oldest date, and the newest date for each corresponding vin.vin datea 1/2/2008a 1/3/2008a 1/4/2008a 1/5/2008b 1/6/2008b 1/7/2008b 1/8/2008b 1/9/2008 Therefore i need to write a delete statement that will get rid of all the entries with dates that are in between.vin datea 1/2/2008a 1/5/2008b 1/6/2008b 1/9/2008 like the above. There should be 2 entries for each vin, one with the most recent date, and one with the oldest. |
|
|
jason7655
Starting Member
24 Posts |
Posted - 2008-12-29 : 22:57:35
|
| I would try to use between min and max. |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-12-29 : 23:37:02
|
| Hi,Try Thisdeletefrom dup where dup.date IN (select b.date from (select vin,min(date) mindate, max(date) maxdate from dup group by vin) across apply (select * from dup where dup.date <> mindate and dup.date <> maxdate and vin = a.vin) b)Jai Krishna |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-12-29 : 23:41:58
|
| declare @tab table(vin varchar(32), date datetime )insert into @tabselect vin,max(date) from urtable group by vinunion allselect vin, min(date) from urtable group by vindelete from urtable where date NOT IN (select date from @tab)select * from urtable |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 00:23:42
|
quote: Originally posted by sqlchiq why doesn't this delete statement work?deletefrom deleteme2 a, (select min(date) mindate, max(date) maxdate, vin from deleteme2 group by vin)bwhere a.vin = b.vin and a.date<>b.mindate and a.date <>b.maxdateWhen its a select* statement it works.basically I have entries with 2 columns, vin and dateThere are many many many identical vins but none with the same date.I want to delete every date in between and keep only the oldest date, and the newest date for each corresponding vin.vin datea 1/2/2008a 1/3/2008a 1/4/2008a 1/5/2008b 1/6/2008b 1/7/2008b 1/8/2008b 1/9/2008 Therefore i need to write a delete statement that will get rid of all the entries with dates that are in between.vin datea 1/2/2008a 1/5/2008b 1/6/2008b 1/9/2008 like the above. There should be 2 entries for each vin, one with the most recent date, and one with the oldest.
DELETE tFROM YourTable tLEFT JOIN (SELECT vin,max(date) as latest FROM YourTable GROUP BY vin)lON l.vin=t.vinAND l.latest=t.dateLEFT JOIN (SELECT vin,min(date) as oldest FROM YourTable GROUP BY vin)oON o.vin=t.vinAND o.oldest =t.dateWHERE l.vin IS NULLAND o.vin IS NULL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 00:25:32
|
and if sql 2005, you can use ROW_NUMBER also to deleteDELETE tFROM(SELECT ROW_NUMBER() OVER (PARTITION BY vin ORDER BY date) AS Inc,ROW_NUMBER() OVER (PARTITION BY vin ORDER BY date DESC) AS DecFROM YourTable)tWHERE t.Inc<>1 AND t.Dec <>1 |
 |
|
|
sqlchiq
Posting Yak Master
133 Posts |
Posted - 2008-12-30 : 12:11:43
|
quote:
DELETE tFROM YourTable tLEFT JOIN (SELECT vin,max(date) as latest FROM YourTable GROUP BY vin)lON l.vin=t.vinAND l.latest=t.dateLEFT JOIN (SELECT vin,min(date) as oldest FROM YourTable GROUP BY vin)oON o.vin=t.vinAND o.oldest =t.dateWHERE l.vin IS NULLAND o.vin IS NULL
one more problem, the dates are all random, for example...1/25/2008 12:45:231/25/2008 11:22:121//12/2008 23:22:11Is there a way to sort this change this to1/25/2008 00:00:001/25/2008 00:00:001//12/2008 00:00:00before, during, and after the delete statement?I do not believe that the following is validmin(dateadd(dd, datediff(dd, 0, YourDateHere), 0))andmax(dateadd(dd, datediff(dd, 0, YourDateHere), 0)) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 12:14:53
|
| did understand why that becomes a problem? dont you just need to delete all execept latest and oldest ones, which will work fine even in above case as max() and min() returns correct values irrespective of actual order of data. |
 |
|
|
sqlchiq
Posting Yak Master
133 Posts |
Posted - 2008-12-30 : 13:34:14
|
quote: Originally posted by visakh16 did understand why that becomes a problem? dont you just need to delete all execept latest and oldest ones, which will work fine even in above case as max() and min() returns correct values irrespective of actual order of data.
Really? will it work correctly as is?I thought SQL server does max and min by the absolute first number, for example in a max,9/12/2008 11:12:23 might be read as later than 10/1/2008 12:22:12 because the 1 in 10 is less than the 9 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 13:38:17
|
quote: Originally posted by sqlchiq
quote: Originally posted by visakh16 did understand why that becomes a problem? dont you just need to delete all execept latest and oldest ones, which will work fine even in above case as max() and min() returns correct values irrespective of actual order of data.
Really? will it work correctly as is?I thought SQL server does max and min by the absolute first number, for example in a max,9/12/2008 11:12:23 might be read as later than 10/1/2008 12:22:12 because the 1 in 10 is less than the 9
it will sort alphabetically based on data type and then take max. so if datatype of field is datetime or smalldatetime, then max() will always return 10/1/2008 12:22:12 as Oct comes after sept while sorting dates.However if your data type happens to be char or varchar, it works exactly as you told. |
 |
|
|
sqlchiq
Posting Yak Master
133 Posts |
Posted - 2008-12-30 : 13:42:56
|
| I see, no wonder my max's and min's were screwed up. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 13:44:40
|
quote: Originally posted by sqlchiq I see, no wonder my max's and min's were screwed up.
thats the reason why its always suggested to use proper datatypes for fields. making your date fields as datetime type will make manipulations and calculations much easier, as sqlserver has several date functions to help you in it. |
 |
|
|
|