Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I was trying to order by my wish. But i m unable to do.
SQL
select * from #tempmonthly with(nolock) order by hub_id(sorted,12,1,20,2,136,3,9,4,1,5,652,6,672,7,129,8,240,9,17,10,14,11,627,12,18,13,574,14,158,15,145,16)

I tried this, but i got error
'hub_id' is not a recognized built-in function name.
Working code is as follows:
SQL
select * from #tempmonthly with(nolock) order by hub_id

Guide me[as i want to arrange by my own order by].

I tried to google it. I got this answer like this
C#
order by decode( sorted, 'F',1, 'C',2, 'A',3, 'B',4, 'G',5, 'D',6, 'E',7, 8 )

So i tried to do that in my code:
C#
select * from #tempmonthly with(nolock) order by hub_id (sorted,12,1,20,2,136,3,9,4,1,5,652,6,672,7,129,8,240,9,17,10,14,11,627,12,18,13,574,14,158,15,145,16)

But it is not working
Posted
Updated 20-Aug-13 2:48am
v5
Comments
Joezer BH 20-Aug-13 8:22am    
Looks like your SQL statements are identical, am I missing something?
anurag19289 20-Aug-13 8:31am    
question is improved...
ZurdoDev 20-Aug-13 8:24am    
The error is clear. It does not know what hub_id is. However, unless I am blind your 2 SQL statements are the same.
anurag19289 20-Aug-13 8:26am    
thanks for your comment.. let me clear it..

query 1
select * from #tempmonthly with(nolock) order by hub_id (sorted,12,1,20,2,136,3,9,4,1,5,652,6,672,7,129,8,240,9,17,10,14,11,627,12,18,13,574,14,158,15,145,16)

query 2
select * from #tempmonthly with(nolock) order by hub_id

anurag19289 20-Aug-13 8:28am    
i tried to google it ,,, i got that sorted part...so immediatey after order by (sorted,)

this starts... but this is not working

1 solution

There is no other way than creating new table ;)
SQL
DECLARE @sortOrder(hub_id INT, ord INT IDENTITY(1,1))

INSERT INTO @sortOrder (hub_id)
SELECT 12
UNION ALL SELECT 1
UNION ALL SELECT 20
UNION ALL SELECT 2
UNION ALL SELECT 136
UNION ALL SELECT 3
UNION ALL SELECT 9
UNION ALL SELECT 4
UNION ALL SELECT 1
UNION ALL SELECT 5
UNION ALL SELECT 652
UNION ALL SELECT 6
UNION ALL SELECT 672
UNION ALL SELECT 7
UNION ALL SELECT 129
UNION ALL SELECT 8
UNION ALL SELECT 240
UNION ALL SELECT 9
UNION ALL SELECT 17
UNION ALL SELECT 10
UNION ALL SELECT 14
UNION ALL SELECT 11
UNION ALL SELECT 627
UNION ALL SELECT 12
UNION ALL SELECT 18
UNION ALL SELECT 13
UNION ALL SELECT 574
UNION ALL SELECT 14
UNION ALL SELECT 158
UNION ALL SELECT 15
UNION ALL SELECT 145
UNION ALL SELECT 16


then to join both tables:
SQL
SELECT t1.*
FROM #tempmonthly AS t1 INNER JOIN @sortOrder AS t2 ON t1.hub_id = t2.hub_id
ORDER BY t2.ord


Another way is to use CASE with ORDER statement, but - in my opinion - this is less elegant ;(
SQL
SELECT *
FROM #tempmonthly
ORDER BY CASE WHEN hub_id = 12 THEN 1
    WHEN hub_id = 1 THEN 2
    WHEN hub_id = 20 THEN 3
    WHEN hub_id = 2 THEN 4
    ...
    ELSE 123 END
 
Share this answer
 
v2
Comments
anurag19289 20-Aug-13 10:07am    
You are really genius :) Both are working perfectly fine... :) :) i liked the first approach
Maciej Los 20-Aug-13 16:12pm    
Thank you ;)
Thanks7872 20-Aug-13 12:19pm    
Hmmmm.....Maciej rocks...:-D Upvoted...!
Maciej Los 20-Aug-13 16:12pm    
Thank you ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900