Solved

Special Sorting

Posted on 2014-01-02
5
247 Views
Last Modified: 2014-01-10
I am having trouble figuring out how to handle the following special sort condition

Here is the sample data (MySQL DB):

salesorder , invoice_date, releasable, invoiced

1000, 2013-01-01, 0, 0
1001, 2013-01-02, 1, 0
1002, 2013-01-03, 0, 1
1003, 2013-01-04, 1, 1
1004, 2013-01-05, 0, 0


When displaying this day on the page

I need to display by:

invoice_date asc

but also the rows where releasable = 0 and invoiceable = 0 need to show before any rows that have releasable=1 or invoiced=1.


The output would need to be:


1000
1004
1001
1002
1003

Any help would be appreciated.

Thanks!
0
Comment
Question by:rjohnsonjr
[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
5 Comments
 
LVL 41

Expert Comment

by:Sharath
ID: 39752485
try this.
select *
  from your_table
 order by case when releasable = 0 and invoiceable = 0 then 0 else 1 end, invoice_date

Open in new window

0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39752494
SELECT salesorder , invoice_date, releasable, invoiced FROM yourtable ORDER BY invoiceable, releasable, invoice_date

The default order is ASC so you don't need to put that there.
0
 
LVL 17

Expert Comment

by:Kent Dyer
ID: 39752526
Is this what you are looking for?

http://sqlfiddle.com/#!3/d0d25/4

Hope this helps
0
 
LVL 41

Accepted Solution

by:
Sharath earned 500 total points
ID: 39752538
Kent, I think rjohnsonjr wants this.
http://sqlfiddle.com/#!3/d0d25/8
0
 
LVL 6

Author Comment

by:rjohnsonjr
ID: 39752539
Sharath_123's solution seems to work best because it maintains the date sort order on all the rows that do not have 0 in releasable and invoiced.

Here is the query:

SELECT salesorder , invoice_date, releasable, invoiced FROM test order by case when releasable = 0 and invoiced = 0 then 0 else 1 end, invoice_date

Open in new window


http://sqlfiddle.com/#!3/d0d25/9
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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