订单在两列中记录ASC [英] Orderby record ASC in two columns

查看:86
本文介绍了订单在两列中记录ASC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在窗口应用程序中以大胆的形式订购OrderBy。我尝试使用这段代码:



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))





这是正确的方法吗?



我正在寻找的是OrderBy(call_case)ASC,以及call_case =(2-Answer)OrderBy( Payment_Status)ASC。





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





我有一个注释,我的帮助文本以nu开头mber喜欢1-No Answer,2-answer,3- not存在



我尝试过:





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:

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

推荐答案

升序顺序是默认顺序。您无需指定它。除此之外,你的代码应运行良好。



我可能会使用CTE,但那只是我。

"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.
;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;



我认为这更容易阅读。我也会避免使用 SELECT * 。这是不好的做法,你不能告诉它不要在返回的数据集中包含你的 CallCaseOrder


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.


这篇关于订单在两列中记录ASC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆