Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
SQL
SELECT CategoryName
FROM Category WHERE CatId IN (55,568,5412,8991,928,3213,142,9250,12330,2983)

I would like the records to be shown in the same order as (55,568,5412,8991,928,3213,142,9250,12330,2983), but I am getting some random order.
Posted
Updated 6-Oct-11 20:35pm
v2
Comments
André Kraak 7-Oct-11 2:35am    
Edited question:
Corrected tags
Spelling/Grammar
Removed shouting
Bala Selvanayagam 7-Oct-11 4:13am    
Where do you get the series (55,568,5412,8991,928,3213,142,9250,12330,2983)? is it retrieved from an another table ? if so can you please give the table structure of that table and the query you are using ?

If that is the case, this should be possible

let me know
Rakesh S S 7-Oct-11 5:01am    
YES CatId coming from some other qry
otherwise it is 2 difficult to get that order
Bala Selvanayagam 7-Oct-11 7:37am    
If CatID is comming from an another query on the order (of 55,568,5412,8991,928,3213,142,9250,12330,2983) then why do not you put a join there on Category?

You should give a full detail, if you needs a fast reponse and your issue to be resolved.

i got it
select * 
from ProductGroups
where GroupID in (24,12,7,14,65)
order by case GroupId 
    when 7 then 1 -- First in ordering
    when 14 then 2 -- Second
    else 3
end
 
Share this answer
 
v2
Comments
Bala Selvanayagam 7-Oct-11 4:17am    
Are we going to hard code the numbers in the case statement and what happens if the number series changes ?
This would do it

SQL
SELECT  CategoryName
FROM    Category
JOIN    (
        SELECT 55 AS CatId UNION
        SELECT 568 UNION
        SELECT 5412 UNION
        SELECT 8991 UNION
        SELECT 928 UNION
        SELECT 3213 UNION
        SELECT 142 UNION
        SELECT 9250 UNION
        SELECT 12330 UNION
        SELECT 2983
        ) AS CatFilter
        ON CatFilter.CatId = Category.CatId
ORDER
BY      CatFilter.CatId
 
Share this answer
 
Comments
Photon_ 17-Feb-12 7:27am    
a long work but just think about hundred rows
I dont think its possible.

If your table contains primary key then
Sql fetches the records and sort the records based on primarykey column

If your table does not contain primary key then
Sql fetches the records and sort the records based on first column in fetched records.
 
Share this answer
 
Comments
Photon_ 17-Feb-12 7:27am    
right

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