Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MSSQL ORDER BY CLAUSE

Posted on 2006-11-06
6
Medium Priority
?
2,361 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
[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
  • 2
6 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 1000 total points
ID: 17881826
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
ID: 17881833
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
ID: 17882025
Oooh. By seconds! Drat!
0
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 

Author Comment

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

Expert Comment

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

Author Comment

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

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

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.
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…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

670 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