Unexpected behaviour of Order by clause
Posted
by Newbie
on Stack Overflow
See other posts from Stack Overflow
or by Newbie
Published on 2010-05-24T03:08:32Z
Indexed on
2010/05/24
3:20 UTC
Read the original article
Hit count: 247
I have a table which looks like
Col1 col2 col3 col4 col5
1 5 1 4 6
1 4 0 3 7
0 1 5 6 3
1 8 2 1 5
4 3 2 1 4
The script is
declare @t table(col1 int, col2 int, col3 int,col4 int,col5 int)
insert into @t
select 1,5,1,4,6 union all
select 1,4,0,3,7 union all
select 0,1,5,6,3 union all
select 1,8,2,1,5 union all
select 4,3,2,1,4
If I do a sorting (ascending), the output is
Col1 col2 col3 col4 col5
0 1 5 6 3
1 4 0 3 7
1 5 1 4 6
1 8 2 1 5
4 3 2 1 4
The query is
Select * from @t
order by col1,col2,col3,col4,col5
But as can be seen that the sorting output is wrong (col2 to col5).
I want the output to be every column being sorted in ascending order i.e.
Col1 col2 col3 col4 col5
0 1 0 1 3
1 3 1 1 4
1 4 2 3 5
1 5 2 4 6
4 8 5 6 7
Why so and how to overcome this?
Thanks in advance
© Stack Overflow or respective owner