Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Half sorting

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 only

Vabhav 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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-03-02 : 06:54:04
GO
CREATE TABLE #temp ( ID int, Code VARCHAR(3) )
GO
INSERT 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'
GO
SELECT * FROM #temp
GO

Want select all the records from above table but want sort only top 5 records means result set should be like below -

1 AAA
2 BBB
4 DDD
7 GGG
9 III
5 EEE
3 CCC
8 HHH
6 FFF



Vabhav T
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-02 : 07:06:43
Why is DDD before CCC?

PBUH
Go to Top of Page

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.
Go to Top of Page

vaibhavktiwari83
Aged Yak Warrior

843 Posts

Posted - 2010-03-02 : 07:13:45
Sort by ID Dear...

Vabhav T
Go to Top of Page

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.
Go to Top of Page

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 t
order by case when sno<=5 then 0 else 1 end,sno,code[/code]

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 AAA
4 DDD
7 GGG
9 III
2 BBB
5 EEE
3 CCC
8 HHH
6 FFF
*/
DECLARE @t TABLE (ID int, Code VARCHAR(3));

INSERT INTO @t
SELECT TOP 5 ID, Code
FROM #temp
ORDER BY Code ASC;

INSERT INTO @t
SELECT *
FROM #temp
WHERE ID NOT IN
(SELECT TOP 5 ID
FROM #temp
ORDER BY Code ASC);

--After:
SELECT * FROM @t
/*
ID Code
----------- ----
1 AAA
2 BBB
3 CCC
4 DDD
5 EEE
7 GGG
9 III
8 HHH
6 FFF
*/
[/code]
Go to Top of Page

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 AAA
4 DDD
7 GGG
9 III
2 BBB
5 EEE
3 CCC
8 HHH
6 FFF
*/
DECLARE @t TABLE (ID int, Code VARCHAR(3));

INSERT INTO @t
SELECT TOP 5 ID, Code
FROM #temp
ORDER BY Code ASC;

INSERT INTO @t
SELECT *
FROM #temp
WHERE ID NOT IN
(SELECT TOP 5 ID
FROM #temp
ORDER BY Code ASC);

--After:
SELECT * FROM @t
/*
ID Code
----------- ----
1 AAA
2 BBB
3 CCC
4 DDD
5 EEE
7 GGG
9 III
8 HHH
6 FFF
*/



OP wants to sort only the top 5 records. Rest of them should be in the same order

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-03-02 : 07:40:28
My previous solution should be

select ID,code from
(
SELECT row_number() over (order by (select 0)) as sno,* FROM #temp
) as t
order by case when sno<=5 then 0 else 1 end,ID


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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.
Go to Top of Page

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 result
1 AAA
2 BBB
4 DDD
7 GGG
9 III
5 EEE
3 CCC
8 HHH
6 FFF


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

ms65g
Constraint Violating Yak Guru

497 Posts

Posted - 2010-03-02 : 07:54:14
It must be okay:

SET NOCOUNT ON
--Befor:
SELECT * FROM #temp

DECLARE @t1 TABLE (ID int, Code VARCHAR(3));
DECLARE @t2 TABLE (ID int, Code VARCHAR(3));

INSERT INTO @t1
SELECT TOP 5 ID, Code
FROM #temp

INSERT INTO @t2
SELECT * FROM @t1 ORDER BY ID ASC

INSERT INTO @t2
SELECT *
FROM #temp
WHERE ID NOT IN
(SELECT ID FROM @t1)

--After:
SELECT * FROM @t2
/*
ID Code
----------- ----
1 AAA
2 BBB
4 DDD
7 GGG
9 III
5 EEE
3 CCC
8 HHH
6 FFF
*/



Go to Top of Page

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 #temp

DECLARE @t1 TABLE (ID int, Code VARCHAR(3));
DECLARE @t2 TABLE (ID int, Code VARCHAR(3));

INSERT INTO @t1
SELECT TOP 5 ID, Code
FROM #temp

INSERT INTO @t2
SELECT * FROM @t1 ORDER BY ID ASC

INSERT INTO @t2
SELECT *
FROM #temp
WHERE ID NOT IN
(SELECT ID FROM @t1)

--After:
SELECT * FROM @t2
/*
ID Code
----------- ----
1 AAA
2 BBB
4 DDD
7 GGG
9 III
5 EEE
3 CCC
8 HHH
6 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

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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 t
order 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 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 t
order 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 1736
The 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 sorting

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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?

example
DROP TABLE #temp
GO
CREATE TABLE #temp ( ID int PRIMARY KEY, Code VARCHAR(3) )
GO
INSERT 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'
GO
SELECT * FROM #temp
GO
select ID,code from
(
SELECT row_number() over (order by (select 0)) as sno,* FROM #temp
) as t
order by case when sno<=5 then 0 else 1 end,ID



Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

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 0

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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.
Go to Top of Page

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.
Go to Top of Page
    Next Page

- Advertisement -