| Author |
Topic |
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-02 : 06:40:57
|
| Is there any way to sort any result set top n rows onlyVabhav T |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-02 : 06:46:37
|
| you can use an autonumber column (ROW_NUMBER())And then use CASE in your ORDER BY clause to order.If you post more detailed description you'll get a more detailed answer.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-02 : 06:54:04
|
| GOCREATE TABLE #temp ( ID int, Code VARCHAR(3) )GOINSERT INTO #temp SELECT 1, 'AAA'UNION ALL SELECT 4, 'DDD'UNION ALL SELECT 7, 'GGG'UNION ALL SELECT 9, 'III'UNION ALL SELECT 2, 'BBB'UNION ALL SELECT 5, 'EEE'UNION ALL SELECT 3, 'CCC'UNION ALL SELECT 8, 'HHH'UNION ALL SELECT 6, 'FFF'GOSELECT * FROM #tempGOWant select all the records from above table but want sort only top 5 records means result set should be like below -1 AAA2 BBB4 DDD7 GGG9 III5 EEE3 CCC8 HHH6 FFFVabhav T |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-03-02 : 07:06:43
|
| Why is DDD before CCC?PBUH |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-02 : 07:13:17
|
There is no reliable order of rows in a ms sql server table.So you cant say "order only the top 5 records" depending on the order they were inserted. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2010-03-02 : 07:13:45
|
| Sort by ID Dear...Vabhav T |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-02 : 07:20:22
|
Your ID won't give you the top 5 you're talking about. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 07:26:02
|
| [code]select ID,code from (SELECT row_number() over (order by (select 0)) as sno,* FROM #temp) as torder by case when sno<=5 then 0 else 1 end,sno,code[/code]MadhivananFailing to plan is Planning to fail |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-02 : 07:33:43
|
| [code]SET NOCOUNT ON--Befor:SELECT * FROM #temp/*ID Code----------- ----1 AAA4 DDD7 GGG9 III2 BBB5 EEE3 CCC8 HHH6 FFF*/DECLARE @t TABLE (ID int, Code VARCHAR(3));INSERT INTO @tSELECT TOP 5 ID, CodeFROM #tempORDER BY Code ASC;INSERT INTO @tSELECT *FROM #tempWHERE ID NOT IN (SELECT TOP 5 ID FROM #temp ORDER BY Code ASC);--After: SELECT * FROM @t/*ID Code----------- ----1 AAA2 BBB3 CCC4 DDD5 EEE7 GGG9 III8 HHH6 FFF*/[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 07:38:34
|
quote: Originally posted by ms65g
SET NOCOUNT ON--Befor:SELECT * FROM #temp/*ID Code----------- ----1 AAA4 DDD7 GGG9 III2 BBB5 EEE3 CCC8 HHH6 FFF*/DECLARE @t TABLE (ID int, Code VARCHAR(3));INSERT INTO @tSELECT TOP 5 ID, CodeFROM #tempORDER BY Code ASC;INSERT INTO @tSELECT *FROM #tempWHERE ID NOT IN (SELECT TOP 5 ID FROM #temp ORDER BY Code ASC);--After: SELECT * FROM @t/*ID Code----------- ----1 AAA2 BBB3 CCC4 DDD5 EEE7 GGG9 III8 HHH6 FFF*/
OP wants to sort only the top 5 records. Rest of them should be in the same orderMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 07:40:28
|
My previous solution should beselect ID,code from (SELECT row_number() over (order by (select 0)) as sno,* FROM #temp) as torder by case when sno<=5 then 0 else 1 end,ID MadhivananFailing to plan is Planning to fail |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-02 : 07:40:51
|
| >>OP wants to sort only the top 5 records. Rest of them should be in the same order<<Please re-read my results! Only TOP 5 record is sorted. and the rest is not any order. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 07:43:06
|
quote: Originally posted by ms65g >>OP wants to sort only the top 5 records. Rest of them should be in the same order<<Please re-read my results! Only TOP 5 record is sorted. and the rest is not any order.
Re-read OP's expected result1 AAA2 BBB4 DDD7 GGG9 III5 EEE3 CCC8 HHH6 FFF MadhivananFailing to plan is Planning to fail |
 |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-02 : 07:54:14
|
It must be okay:SET NOCOUNT ON--Befor:SELECT * FROM #tempDECLARE @t1 TABLE (ID int, Code VARCHAR(3));DECLARE @t2 TABLE (ID int, Code VARCHAR(3));INSERT INTO @t1SELECT TOP 5 ID, CodeFROM #tempINSERT INTO @t2SELECT * FROM @t1 ORDER BY ID ASCINSERT INTO @t2SELECT *FROM #tempWHERE ID NOT IN (SELECT ID FROM @t1)--After: SELECT * FROM @t2/*ID Code----------- ----1 AAA2 BBB4 DDD7 GGG9 III5 EEE3 CCC8 HHH6 FFF*/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 08:02:46
|
quote: Originally posted by ms65g It must be okay:SET NOCOUNT ON--Befor:SELECT * FROM #tempDECLARE @t1 TABLE (ID int, Code VARCHAR(3));DECLARE @t2 TABLE (ID int, Code VARCHAR(3));INSERT INTO @t1SELECT TOP 5 ID, CodeFROM #tempINSERT INTO @t2SELECT * FROM @t1 ORDER BY ID ASCINSERT INTO @t2SELECT *FROM #tempWHERE ID NOT IN (SELECT ID FROM @t1)--After: SELECT * FROM @t2/*ID Code----------- ----1 AAA2 BBB4 DDD7 GGG9 III5 EEE3 CCC8 HHH6 FFF*/
Yes. As it is posted in SQL Server 2005 forum you can make use of row_number() function as I used so that you can avoid using two extra tables MadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-02 : 08:04:46
|
Hi madhivanan.What does the segment highlighted in red from your code do?select ID,code from (SELECT row_number() over (order by (select 0)) as sno,* FROM #temp) as torder by case when sno<=5 then 0 else 1 end,ID I assumed that from the OP's INSERT x SELECT y UNION ALL SELECT z.... approach there wouldn't be a guaranteed order in the way that he was expecting ( the first line of his SELECT construct first, then the next, etc, etc).How does this pull out the required output?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 08:07:57
|
quote: Originally posted by Transact Charlie Hi madhivanan.What does the segment highlighted in red from your code do?select ID,code from (SELECT row_number() over (order by (select 0)) as sno,* FROM #temp) as torder by case when sno<=5 then 0 else 1 end,ID I assumed that from the OP's INSERT x SELECT y UNION ALL SELECT z.... approach there wouldn't be a guaranteed order in the way that he was expecting ( the first line of his SELECT construct first, then the next, etc, etc).How does this pull out the required output?Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
The red part make sure that data are ordered as such in the table so that no column will be considered for sortingMadhivananFailing to plan is Planning to fail |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2010-03-02 : 08:15:03
|
Ah cool -- so this only works on HEAP tables?I take it if there is a clustered index then that will override your SELECT 0?exampleDROP TABLE #tempGOCREATE TABLE #temp ( ID int PRIMARY KEY, Code VARCHAR(3) )GOINSERT INTO #temp SELECT 1, 'AAA'UNION ALL SELECT 4, 'DDD'UNION ALL SELECT 7, 'GGG'UNION ALL SELECT 9, 'III'UNION ALL SELECT 2, 'BBB'UNION ALL SELECT 5, 'EEE'UNION ALL SELECT 3, 'CCC'UNION ALL SELECT 8, 'HHH'UNION ALL SELECT 6, 'FFF'GOSELECT * FROM #tempGOselect ID,code from (SELECT row_number() over (order by (select 0)) as sno,* FROM #temp) as torder by case when sno<=5 then 0 else 1 end,ID Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 08:24:29
|
| I think you can't assume it. Eventhough the table has clustered index default order is not gauranteed. So based on data sometimes it may or may not override select 0MadhivananFailing to plan is Planning to fail |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2010-03-02 : 08:25:10
|
| Webfred has it right. As there are no guarantees to unspecified sort order, the OP's requested output can never actually be achieved reliably no matter what tricks you use. The requirement is wrong - it implies there is a 'correct' answer for the rest of the rows and there can never be.You might as well just order by code all the way through and dispense with all the magic voodoo that will probably not actually work. |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-02 : 09:03:53
|
quote: Originally posted by LoztInSpace Webfred has it right. As there are no guarantees to unspecified sort order, the OP's requested output can never actually be achieved reliably no matter what tricks you use. The requirement is wrong - it implies there is a 'correct' answer for the rest of the rows and there can never be.You might as well just order by code all the way through and dispense with all the magic voodoo that will probably not actually work.
Thank you so much for posting that so I don't have to post it again  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
Next Page
|