在网格视图中以特定顺序显示记录 [英] showing records in specific order in grid view

查看:71
本文介绍了在网格视图中以特定顺序显示记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为tblcomp的表,其中包含以下字段:
id
名称
sts



sts 中有值 P,A,D,R
我想在网格视图中显示所有记录,但是优先显示先显示 sts 的记录,然后再显示 sts A ,然后加上 sts D .


速度为:
P
A
D
R



我试图使用order by,但它按特定顺序排序.
请提出建议.


在此先感谢

I have a table named tblcomp with fields:
id
name
sts



In sts there are values P,A,D,R
I want to show all the records in grid view, but in precedence that records with sts are showed first, then with sts A , after that with sts D.


Pecedence is as:
P
A
D
R



I was trying to use order by but it sort in a specific order.
Please suggest how it can be done.


Thanks in advance

推荐答案

您可以使用Common表进行救援,

You could have Common table to rescue,

WITH CTE
AS
(
select id,name,CASE [sts] WHEN 'P' THEN 1 WHEN 'A' THEN 2 WHEN 'D' THEN 3 WHEN 'R' THEN 4 END statuscode from table
)
select * from CTE ORDER BY statuscode



如果有帮助,请 投票 接受答案 .



Please vote and Accept Answer if it Helped.


也许有另一个定义您的状态代码的表.

例如

tblStatus
statusId
statusCode
statusOrder

您的记录可以定义为

Maybe have another table that defines your status codes.

e.g

tblStatus
statusId
statusCode
statusOrder

Your records could be defined as

statusId      statusCode    statusOrder
1             D             3
2             A             2
3             P             1
4             R             4


然后,您可以只使用标准SQL并联接表+使用statusOrder列上的订单


Then you can just use standard SQL and join the tables + use an order by on the statusOrder column

select * from tblcomp C inner join tblStatus S on C.sts = S.statusCode ORDER BY statusOrder



如果要执行此操作,最好将tblStatus中的ID存储在tblComp中,而不是在状态代码中存储-但以上内容演示了该技术



If you''re going to do this, better to store the ID from tblStatus in tblComp rather than the status code - but the above demonstrates the technique


U正在使用sql select查询来填充网格...只需在查询的末尾添加[order by column name(asc/desc)]子句,具体取决于您要如何排序....... ..:laugh:
U are using a sql select query to populate the grid...Just add a [order by column name (asc/desc)] clause at the end of the query depend on how u want to sort.................. :laugh:


这篇关于在网格视图中以特定顺序显示记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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