| Author |
Topic |
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-11-10 : 14:51:34
|
| Hello All,i have a leaveTemp table with below dataUserID, UserName, fromdate,todate101, wiran, 11/7/2008, 11/30/2008 102, Madison,11/1/2008, 12/1/2008 103, Sinka , 11/3/2008, 11/11/2008104, Padis, 10/1/2008, 11/21/2008 105, Jikki, 9/10/2008, 9/25/2008 105, Jikki, 10/6/2008, 10/18/2008 from the above data how can i remove the duplicate record(based on userid) and needs to remove the one, where getdate not in FromDate & toDate example: in the above data 5th record needs to be delete because today (10/10/2008) not between 9/10/2008 and 9/25/2008please give me an ideaThanks & Best Regardsasin |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-11-10 : 15:16:23
|
DELETEFROM leaveTempWHERE GETDATE() NOT BETWEEN fromDate AND toDate E 12°55'05.63"N 56°04'39.26" |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2008-11-10 : 15:27:47
|
code:declare @temp table(UserID int ,UserName nvarchar(100),fromdate datetime,todate datetime)insert into @temp select 101, 'wiran', '11/7/2008', '11/30/2008' union allselect 102, 'Madison','11/1/2008', '12/1/2008' union allselect 103, 'Sinka', '11/3/2008', '11/11/2008' union allselect 104, 'Padis', '10/1/2008', '11/21/2008' union allselect 105, 'Jikki', '9/10/2008', '9/25/2008' union allselect 105, 'Jikki', '11/6/2008', '11/18/2008'select * from @temp delete from @tempwhereexists (select t2.userid from @temp as t2 where userid = t2.userid group by t2.userid having count(userid) > 1)and getdate() not between fromdate and todateselect * from @temp |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-11-10 : 16:12:19
|
quote: DELETEFROM leaveTempWHERE GETDATE() NOT BETWEEN fromDate AND toDate
Hello Pesso,Thank you for your replyhowever, your suggestion will reomve rows of all table records, i need to apply this condition only for duplicate recordsnot for whole tablesBest Regardsasin |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-11-10 : 16:13:17
|
| Hello Slimt,thank you for your reply,with your query, it is removing all duplicate rows, regardless of fromdate & todateCan you please adviceBest Regardsasin |
 |
|
|
GhantaBro
Posting Yak Master
215 Posts |
Posted - 2008-11-10 : 16:16:30
|
| DELETEFROM leaveTempWHERE GETDATE() NOT BETWEEN fromDate AND toDateand userID in (select userID from leaveTemp group by userID having count(*) > 1 ) |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2008-11-10 : 16:25:21
|
quote: Originally posted by GhantaBro DELETEFROM leaveTempWHERE GETDATE() NOT BETWEEN fromDate AND toDateand userID in (select userID from leaveTemp group by userID having count(*) > 1 )
Hello GhantaBroit is worked very wellthank you for your replyBest Regardsasin |
 |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-11-10 : 16:36:55
|
quote: Originally posted by dhani Hello Slimt,thank you for your reply,with your query, it is removing all duplicate rows, regardless of fromdate & todateCan you please adviceBest Regardsasin
Slimt's sql works the same logic as Ghanta's and better. You must copied wrong. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-11-10 : 18:32:30
|
| For SQL 2005:Delete T from(select row_number() over(partition by UserID order by UserID) as row_number, * from tablewhere getdate() not between fromdate and todate)T |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-10 : 23:06:18
|
quote: Originally posted by sodeep For SQL 2005:Delete T from(select row_number() over(partition by UserID order by UserID) as row_number, * from tablewhere getdate() not between fromdate and todate)T
whats the purpose of row_numebr here? you're not using it for anything. |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-11-10 : 23:11:25
|
quote: Originally posted by sodeep For SQL 2005:Delete T from(select row_number() over(partition by UserID order by UserID) as row_number, * from tablewhere getdate() not between fromdate and todate)T
You are right Visakh!! Thanks. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-10 : 23:12:23
|
Cheers |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-11 : 01:53:51
|
| But as per OPs initial post i dont think we need row number here. It seems like he just need to get record between the correct date range. Only if there's a chance of duplicate record on same date range, we require this. |
 |
|
|
|