| Author |
Topic |
|
allan8964
Posting Yak Master
249 Posts |
Posted - 2012-01-04 : 12:59:49
|
| Hi there,select top 10 SalesDate from table1 order by SalesDate descI need get the 10th SalesDate and put it in another statement likedelete from table1 where SalesDate < 10th_SalesDateHow can I solve this? Thanks in advance. |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-04 : 13:14:51
|
| [code]delete tfrom (select row_number() over (order by SalesDate desc) as rnfrom table1 )twhere rn>10[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-01-04 : 13:41:46
|
I think they want to delete everything BELOW the 10th occurrenceCREATE TABLE #table1 (SalesDate date)GOINSERT INTO #table1 (SalesDate)SELECT '1/1/2012' UNION ALLSELECT '2/1/2012' UNION ALLSELECT '3/1/2012' UNION ALLSELECT '4/1/2012' UNION ALLSELECT '5/1/2012' UNION ALLSELECT '6/1/2012' UNION ALLSELECT '7/1/2012' UNION ALLSELECT '8/1/2012' UNION ALLSELECT '9/1/2012' UNION ALLSELECT '10/1/2012' UNION ALLSELECT '11/1/2012' UNION ALLSELECT '12/1/2012'GOSELECT * FROM #table1GOdelete tfrom (select row_number() over (order by SalesDate desc) as rnfrom #table1 )twhere rn>10GOSELECT * FROM #table1GODROP TABLE #table1GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
allan8964
Posting Yak Master
249 Posts |
Posted - 2012-01-04 : 14:40:05
|
| thanks gentlemen. I think this only returns top 1 of the series:delete from table1 where SalesDate < (select Top 1 SalesDate FROM (select top 10 SalesDate from table1 order by SalesDate desc) AS XXX)and this one works:delete tfrom (select row_number() over (order by SalesDate desc) as rnfrom table1 )twhere rn>10 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
allan8964
Posting Yak Master
249 Posts |
Posted - 2012-01-04 : 19:22:27
|
| Yes sir, I need only one but not top one, I need the BOTTOM one. Your code returns 1st NOT 10th! Think about it:1) You must use DESC to get top ones like "select top 10 SalesDate from table1 order by SalesDate desc";2) after that this piece of codes "select Top 1 SalesDate FROM" returns TOP one from the above codes.3) You use "delete from table1 where SalesDate < " to erase ..., Actually if I have 20 SalesDate then your codes delete all rows except the ones with the latest date. This is not what I want. I want to delete all which ealier than the earliest from the top 10 latest date. Your code will work if we have Bottom in DML state to specify the LAST set of rows. then we can do:delete from table1 where SalesDate < (select BOTTOM 1 SalesDate FROM (select top 10 SalesDate from table1 order by SalesDate desc) AS XXX)Just kidding. Maybe you think the 10th is the latest one, well, if that's the case then I can just do "delete from table1 where SalesDate < (select Max(SalesDate) from table1)" ... so I think using row_num as visakh16 instructed is the only way ...anyway I appreciate your time. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-01-05 : 09:50:30
|
quote: Originally posted by X002548 I think they want to delete everything BELOW the 10th occurrenceCREATE TABLE #table1 (SalesDate date)GOINSERT INTO #table1 (SalesDate)SELECT '1/1/2012' UNION ALLSELECT '2/1/2012' UNION ALLSELECT '3/1/2012' UNION ALLSELECT '4/1/2012' UNION ALLSELECT '5/1/2012' UNION ALLSELECT '6/1/2012' UNION ALLSELECT '7/1/2012' UNION ALLSELECT '8/1/2012' UNION ALLSELECT '9/1/2012' UNION ALLSELECT '10/1/2012' UNION ALLSELECT '11/1/2012' UNION ALLSELECT '12/1/2012'GOSELECT * FROM #table1GOdelete tfrom (select row_number() over (order by SalesDate desc) as rnfrom #table1 )twhere rn>10GOSELECT * FROM #table1GODROP TABLE #table1GO Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/
thats exactly what my query does. it numbers the records based on descending order of saledate and deletes everything > 10 (ie below 10th greatest date)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|