Solved

SQL Custom Sort Order

Posted on 2009-06-28
3
411 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Query Task 11 44
What Is an Error? 2 29
Need to replicate a Log table 4 14
Fee for Crystal Reports Consulting 5 22
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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