SQL Order By Two Cases

Posted on 2013-09-27
Medium Priority
Last Modified: 2013-10-02
If I have three columns in my dataset (price, quantity, date).  How do I allow the user to sort the data based on any and all three columns?  For example:

Order by:
price -> DESC
price -> DESC, quantity->DESC
quantity-> DESC
date -> DESC
Question by:Latitude34
  • 3
LVL 41

Expert Comment

ID: 39529408
Provide more information. Do you want to generate the query based on user sort option or do you want to sort the data in UI?

Author Comment

ID: 39529413
I need to implement paging so I'm thinking that I'll sort the data upon querying it from the DB.

The record set is going to be quite large so if there's a better way, I'm open to it.

Accepted Solution

Latitude34 earned 0 total points
ID: 39529443
Unless there is a more efficient way, I think I got it. Thanks.

WHEN @price = '1' THEN p END DESC,
WHEN @quantity= '1' THEN q END DESC,
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39530293
Unless there is a more efficient way, I think I got it.
That should be fine provided price has the same data type as quantity.  Date of course is quite another story and will require some additional work, but I suspect you have already figured that out.

Author Closing Comment

ID: 39539265
I figured it out.

Featured Post

Build your data science skills into a career

Are you ready to take your data science career to the next step, or break into data science? With Springboard’s Data Science Career Track, you’ll master data science topics, have personalized career guidance, weekly calls with a data science expert, and a job guarantee.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
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.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

627 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