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
Solved

MSSQL ORDER BY CLAUSE

Posted on 2006-11-06
6
2,340 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 143

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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
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…

860 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