Sharad Sharad - 7 months ago 9
SQL Question

SQL Query with specific Order By format

I have a table with following values

OperationCode OperationDesc OperationOrder
Repl Remove 1
R&I Remove 1
Ovrh Remove 1
Refn Prep 1
Repl Replace 2
R&I Install 2
Ovrh Install 2
Refn Paint 2


I want to write a query to get following result order. See in OperationOrder Column

OperationCode OperationDesc OperationOrder
Repl Remove 1
Repl Replace 2
R&I Remove 1
R&I Install 2
Ovrh Remove 1
Ovrh Install 2
Refn Prep 1
Refn Paint 2


I am using sql server 2005.

Answer

Here is SQLFiddel Demo

Below is the Sample Query Which You can try

SELECT * FROM temp
ORDER BY 
CASE OperationCode 
     WHEN 'Repl' THEN 1
     WHEN 'R&I' THEN 2
     WHEN 'Ovrh' THEN 3
     WHEN 'Refn' THEN 4
END, OperationOrder