1

hi i have an sql statement that returns rows from a table in descending order, the column i am ordering by is requestStatus. the requestStatus can be 3 values pending, approved and rejected . i would like to alter the sql if possible to order by pending then approved and then rejected. my sql statement is below. i am using asp.net razor view engine. can anyone point me in the right direction ?

   var dbCommand2 = "SELECT * FROM LeaveRequests WHERE email = @0 ORDER BY WHERE requestStatus DESC";
   var rows1 = db.Query(dbCommand2, theUserName);
1
  • What is that WHERE after ORDER BY? Commented Apr 30, 2014 at 22:51

1 Answer 1

3

Use CASE in your SELECT statement:

SELECT *, CASE requestStatus
   WHEN 'pending' THEN 0
   WHEN 'approved' THEN 1
   WHEN 'rejected' THEN 2
   END  AS requestStatusOrder
FROM LeaveRequests
WHERE email = @0
ORDER BY requestStatusOrder
Sign up to request clarification or add additional context in comments.

2 Comments

You can just put the case directly in the order by and not have to add an additional column to the select.
Hi dave, great thanks for help , that works perfectly . i'll will also try your way gordon , thanks everyone for their help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.