Search code examples
c#form-load

OrderBy record ASC in two columns


I'm trying to OrderBy in form loud my columns in windows application. I try to use this code:

using (SqlCommand sqlcomm = new SqlCommand("SELECT * FROM remaining WHERE username=@username and status=@status and company_status=@company_status ORDER BY call_case ASC , Payment_Status ASC", sqlconn))

Is that the right way to do it?

What I'm looking for is to OrderBy (call_case) ASC, and when call_case= (2-Answer) OrderBy (Payment_Status) ASC .

 (  call_case ), ( Payment_Status )

   null    ,    null 

   1-No Answer ,    null

   2-answer    ,    1-Promise Payment

   2-answer    ,    2-Have Problem

   2-answer    ,    3-Reject Payment

   3- not Exist ,      null

i have a note it my be help the text start with number like 1-No Answer , 2-answer , 3- not Exist


Solution

  • You can use a CASE expression along with ORDER BY like

    SELECT * FROM remaining 
    WHERE username=@username 
    and status=@status 
    and company_status=@company_status 
    ORDER BY      
       case when call_case='Answer' then 0 else 1 end ASC,      
       Payment_Status ASC