| Author |
Topic |
|
landau66
Yak Posting Veteran
61 Posts |
Posted - 2008-08-22 : 06:47:28
|
| hello everyone!I have a table with stockprices per second:id, sDatetime, pricevery seldomly i have a doulbe row which i can see by a double entry in the column sDatetime. I filter this double entries out by:select sdatetime from testen where sdatetime =(select sdatetime from testen group by sdatetime having count(sdatetime) >1)result would be:2008-03-26 14:11:17.0002008-03-26 14:11:17.000or2008-03-26 14:11:17.0002008-03-26 14:11:17.0002008-03-26 14:11:17.000if i would have three times the same entrytop1 i want to keep, the other entries i want to delete. how is that possible without a loop or cursor????data:16525 2008-03-26 14:11:00.000 6510.7116526 2008-03-26 14:11:07.000 6510.6616527 2008-03-26 14:11:08.000 6510.6616643 2008-03-26 14:11:09.000 6510.7016640 2008-03-26 14:11:10.000 6510.6916534 2008-03-26 14:11:15.000 6510.7316535 2008-03-26 14:11:16.000 6510.6916536 2008-03-26 14:11:17.000 6510.6916538 2008-03-26 14:11:17.000 6510.8216541 2008-03-26 14:11:17.000 6510.7216542 2008-03-26 14:11:23.000 6510.8316642 2008-03-26 14:11:26.000 6510.8016547 2008-03-26 14:11:28.000 6510.8316548 2008-03-26 14:11:29.000 6510.83many thanks in advance and greetings from viennalandau |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2008-08-22 : 06:56:19
|
| What version of SQL?Ryan Randall Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-08-22 : 07:13:45
|
| If using sql server 2005, declare @TableVar table ( ID int identity(1,1), sdatetime datetime )Insert @TableVarSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:15.000' union allSelect '2008-03-26 14:11:15.000' Select * from @TableVarDELETE tbl From(select ROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID',sdatetime from @TableVar where ID IN(1,2,3) ) tbl where ID<>1Select * from @TableVarHope this is what you want. Above will delete only if there are exactly 3 duplicate entries. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-22 : 07:27:06
|
quote: Originally posted by sunil If using sql server 2005, declare @TableVar table ( ID int identity(1,1), sdatetime datetime )Insert @TableVarSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:15.000' union allSelect '2008-03-26 14:11:15.000' Select * from @TableVarDELETE tbl From(select ROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID',sdatetime from @TableVar where ID IN(1,2,3) ) tbl where ID<>1Select * from @TableVarHope this is what you want. Above will delete only if there are exactly 3 duplicate entries.
You cant use alias directly like this.I dont think you really require the where.The outside condition is enough. |
 |
|
|
landau66
Yak Posting Veteran
61 Posts |
Posted - 2008-08-22 : 07:27:40
|
| i use sql server 2005 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-22 : 07:29:47
|
or use this if sql 2000DELETE tFROm YourTable tINNER JOIN (SELECT MAX(id) AS MaxID,sDatetime FROM YourTable GROUP BY sDatetime)tmpON tmp.sDatetime=t.sDatetimeAND tmp.MaxID=t.id |
 |
|
|
sunil
Constraint Violating Yak Guru
282 Posts |
Posted - 2008-08-22 : 07:35:43
|
quote: Originally posted by visakh16
quote: Originally posted by sunil If using sql server 2005, declare @TableVar table ( ID int identity(1,1), sdatetime datetime )Insert @TableVarSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:15.000' union allSelect '2008-03-26 14:11:15.000' Select * from @TableVarDELETE tbl From(select ROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID',sdatetime from @TableVar where ID IN(1,2,3) ) tbl where ID<>1Select * from @TableVarHope this is what you want. Above will delete only if there are exactly 3 duplicate entries.
You cant use alias directly like this.I dont think you really require the where.The outside condition is enough.
I have put where condition as only duplicate records with 3 entries should be deleted. I thought O/P doesnot want record with 2 entries to be deleted.Also, can yo please elaborate on You cant use alias directly.In your query, should n't tmp.MaxID=t.id be tmp.MaxID<>t.id as = condition will delete top most record. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-22 : 09:24:08
|
quote: Originally posted by sunil
quote: Originally posted by visakh16
quote: Originally posted by sunil If using sql server 2005, declare @TableVar table ( ID int identity(1,1), sdatetime datetime )Insert @TableVarSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:15.000' union allSelect '2008-03-26 14:11:15.000' Select * from @TableVarDELETE tbl From(select ROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID',sdatetime from @TableVar where ID IN(1,2,3) ) tbl where ID<>1Select * from @TableVarHope this is what you want. Above will delete only if there are exactly 3 duplicate entries.
You cant use alias directly like this.I dont think you really require the where.The outside condition is enough.
I have put where condition as only duplicate records with 3 entries should be deleted. I thought O/P doesnot want record with 2 entries to be deleted.Also, can yo please elaborate on You cant use alias directly.In your query, should n't tmp.MaxID=t.id be tmp.MaxID<>t.id as = condition will delete top most record.
By ID in where clause what did you mean?ID column or ID of row_number?MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-22 : 09:47:02
|
quote: Originally posted by sunil
quote: Originally posted by visakh16
quote: Originally posted by sunil If using sql server 2005, declare @TableVar table ( ID int identity(1,1), sdatetime datetime )Insert @TableVarSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:15.000' union allSelect '2008-03-26 14:11:15.000' Select * from @TableVarDELETE tbl From(select ROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID',sdatetime from @TableVar where ID IN(1,2,3) ) tbl where ID<>1Select * from @TableVarHope this is what you want. Above will delete only if there are exactly 3 duplicate entries.
You cant use alias directly like this.I dont think you really require the where.The outside condition is enough.
I have put where condition as only duplicate records with 3 entries should be deleted. I thought O/P doesnot want record with 2 entries to be deleted.Also, can yo please elaborate on You cant use alias directly.In your query, should n't tmp.MaxID=t.id be tmp.MaxID<>t.id as = condition will delete top most record.
yup... just read earlier post once more. i think what OP needs is thisDELETE tFROm YourTable tLEFT JOIN (SELECT MAX(id) AS MaxID,sDatetime FROM YourTable GROUP BY sDatetime)tmpON tmp.sDatetime=t.sDatetimeAND tmp.MaxID=t.idWHERE tmp.MaxID IS NULL |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2008-08-22 : 09:48:03
|
You could put a unique constraint (or primary key) on the table to prevent dupes from getting in in the first place Be One with the OptimizerTG |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-22 : 09:48:50
|
quote: Originally posted by sunil
quote: Originally posted by visakh16
quote: Originally posted by sunil If using sql server 2005, declare @TableVar table ( ID int identity(1,1), sdatetime datetime )Insert @TableVarSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:17.000' union allSelect '2008-03-26 14:11:15.000' union allSelect '2008-03-26 14:11:15.000' Select * from @TableVarDELETE tbl From(select ROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID',sdatetime from @TableVar where ID IN(1,2,3) ) tbl where ID<>1Select * from @TableVarHope this is what you want. Above will delete only if there are exactly 3 duplicate entries.
You cant use alias directly like this.I dont think you really require the where.The outside condition is enough.
I have put where condition as only duplicate records with 3 entries should be deleted. I thought O/P doesnot want record with 2 entries to be deleted.Also, can yo please elaborate on You cant use alias directly.In your query, should n't tmp.MaxID=t.id be tmp.MaxID<>t.id as = condition will delete top most record.
What i meant was you cant use ID IN(1,2,3) if ID is actually the alias created in select list like belowROW_NUMBER() OVER(PARTITION BY sdatetime ORDER BY sdatetime DESC) AS 'ID' |
 |
|
|
|