?
Solved

SQL Custom Sort Order

Posted on 2009-06-28
3
Medium Priority
?
418 Views
Last Modified: 2012-05-07
I have a table column 'STATUS' with the following possible Data.
STATUS
========
Active
Inactive
Cancelled
Closed
Pending

I want to Sort the table, but placing Closed on top, and Cancelled last, the other Status type should be Sorted by a Date column.

Is it possible to achieve this in SQL? I'm using SQL 2005.

SELECT * from TABLE_A ORDER BY (Closed on top, Cancelled below, others by Date)

0
Comment
Question by:dennisdominic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 2000 total points
ID: 24733339
SELECT * from TABLE_A
ORDER BY Case WHEN Status = 'Closed' then 99 WHEN 'Cancelled' THEN -99 Else 0 END DESC,[Date]


0
 
LVL 1

Author Closing Comment

by:dennisdominic
ID: 31597776
Fast and Acccurate.
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24733352
I am assumin you have data column, so you can do something like this:
Select *, CASE WHEN Status = 'Closed' Then Cast('1700-01-01' as Datetime)
                           WHEN Status = 'Cancelled' Then Cast('3000-01-01' as Datetime)
                           ELSE DateCol
                END as SortOrder
From Table_A
Order by CASE WHEN Status = 'Closed' Then Cast('1700-01-01' as Datetime)
                           WHEN Status = 'Cancelled' Then Cast('3000-01-01' as Datetime)
                           ELSE DateCol
                END


0

Featured Post

Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question