Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to OrderBy in form loud my columns in windows application. I try to use this code:

C#
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

What I have tried:

C#
SELECT * FROM remaining 
WHERE username=@username 
and status=@status 
and company_status=@company_status 
ORDER BY      
   case when call_case='2-Answer' then 0 else 1 end ASC,      
   Payment_Status ASC
Posted
Updated 11-Mar-19 3:46am
Comments
MadMyche 11-Mar-19 7:55am    
Basically you got it, good job. Does it run?

1 solution

"Ascending" order is the default. You don't need to specify it. Beyond that, your code should run fine.

I would probably use a CTE, but that's just me.
SQL
;with cte as
(
    SELECT *,
           CASE WHEN call_case = '2-Answer' THEN 0 ELSE 1 END AS CallCaseOrder
    FROM   dbo.remaining 
    WHERE  username = @username 
    AND    status = @status 
    AND    company_status = @company_status,
)
SELECt * FROM cte
ORDER BY CallCaseOrder, PaymentStatus;

I think that's much easier to read. I also would avoid using "SELECT *". It's bad practice, and you can't tell it not to include your CallCaseOrder in the returned dataset.
 
Share this answer
 
v4

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