Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted
Updated 19-Oct-10 21:48pm
v4

You could have Common table to rescue,

SQL
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.
 
Share this answer
 
v2
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

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


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
 
Share this answer
 
v2
Comments
Hiren solanki 20-Oct-10 4:45am    
Yeah, dylan that could be the one way.
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:
 
Share this answer
 

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