Solved

MSSQL ORDER BY CLAUSE

Posted on 2006-11-06
6
2,315 Views
Last Modified: 2012-05-05
Here is my query.

select field_a, field_b, field_c, field_d from table_abc
order by field_b, field_c,
   CASE field_a
  WHEN 'D' THEN 1
  WHEN' N' THEN 2
  WHEN 'C' THEN 3
 else 99 END ASC

That is very straight forward. The tricky part is field_d. (field_d is a date type)

If field_a is 'D', I want to sort it by  DESC.
Otherwise, I want to sort it by ASC.

I tried
select field_a, field_b, field_c, field_d from table_abc
order by field_b, field_c,
   CASE field_a
  WHEN 'D' THEN 1
  WHEN' N' THEN 2
  WHEN 'C' THEN 3
 else 99 END ASC,
   CASE field_a
   WHEN 'D' THEN field_d DESC
  ELSE field_d ASC END

But I got a syntax error.

Any idea?

thank you.

0
Comment
Question by:vijitc
  • 3
  • 2
6 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 250 total points
Comment Utility
select field_a, field_b, field_c, field_d from table_abc
order by field_b, field_c,
   CASE field_a
  WHEN 'D' THEN 1
  WHEN' N' THEN 2
  WHEN 'C' THEN 3
 else 99 END ASC,
   CASE field_a WHEN 'D' THEN field_d ELSE NULL END DESC,
   CASE field_a WHEN 'D' THEN NULL ELSE field_d END ASC
0
 
LVL 16

Expert Comment

by:muzzy2003
Comment Utility
Try something like this:

select field_a, field_b, field_c, field_d from table_abc
order by field_b, field_c,
   CASE field_a
  WHEN 'D' THEN 1
  WHEN' N' THEN 2
  WHEN 'C' THEN 3
 else 99 END ASC,
CASE when field_a = 'D' then field_d else '' end desc,
CASE when field_a = 'D' then '' else field_d end asc
0
 
LVL 16

Expert Comment

by:muzzy2003
Comment Utility
Oooh. By seconds! Drat!
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:vijitc
Comment Utility
Thank you for both angelIII and muzzy2003.
How to do same query in oracle?
0
 
LVL 16

Expert Comment

by:muzzy2003
Comment Utility
Oracle's case statement has pretty much the same syntax, I think. Try exactly the same thing.
0
 

Author Comment

by:vijitc
Comment Utility
muzzy2003, You are correct. The same query works in oracle. Thank you.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now