Solved

MSSQL ORDER BY CLAUSE

Posted on 2006-11-06
6
2,332 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
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

770 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