Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,


in my table i am having a column STATUS which is having records like this

ID NAME STATUS
101 ABCD 1
102 XYZ 2
103 XXX 3
104 DEF 4

i want to get output in the following format

ID NAME STATUS
102 XYZ 2
101 ABCD 1
104 DEF 4
103 XXX 3
Posted
Comments
ArunRajendra 18-Jul-13 0:31am    
What is the criteria for getting this order?
ali from hyd 18-Jul-13 0:36am    
for my ticketing functionality i am having status as 1-Open ,2-Reply , 3-Closed ,4-Reopen.

in grid i want to display the records it in the following order 2,1,4,3
Sergey Alexandrovich Kryukov 18-Jul-13 0:40am    
Again, what are the ordering criteria? Did you understand the question? Not an example, the criteria?
—SA
ali from hyd 18-Jul-13 0:54am    
i have to display the Ticket which is in Replied Status first
Open Status second
Reopen Status Third
Closed Status Fourth

Try this code. Replace @t with your table name.

SQL
select id,NAME,
case
when status=2 then 1
when status=1 then 2
when status=4 then 3
when status=3 then 4
end as newstatus
from @t
order by newstatus
 
Share this answer
 
Comments
ali from hyd 18-Jul-13 1:50am    
nice thank u
ArunRajendra 18-Jul-13 1:58am    
Can you please mark it as answered.
ArunRajendra 18-Jul-13 1:57am    
welcome
Assuming you have a status table that contains all the provided status. You can add a column in that status table named ORDERING_INFO.

SQL
ALTER TABLE STATUS ADD ORDERING_INFO INT NULL

UPDATE STATUS SET ORDERING_INFO = 2 WHERE STATUS_ID = 1;
UPDATE STATUS SET ORDERING_INFO = 1 WHERE STATUS_ID = 2;
UPDATE STATUS SET ORDERING_INFO = 4 WHERE STATUS_ID = 3;
UPDATE STATUS SET ORDERING_INFO = 3 WHERE STATUS_ID = 4;


You can then modify your select query like this

SQL
SELECT ID,NAME
FROM   TABLE 
       INNER JOIN STATUS ON TABLE.STATUS = STATUS.STATUS_ID
ORDER BY STATUS.ORDERING_INFO
 
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