Solved

Special Sorting

Posted on 2014-01-02
5
235 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
5 Comments
 
LVL 40

Expert Comment

by:Sharath
Comment Utility
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 82

Expert Comment

by:Dave Baldwin
Comment Utility
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
Comment Utility
Is this what you are looking for?

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

Hope this helps
0
 
LVL 40

Accepted Solution

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

Author Comment

by:rjohnsonjr
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Read about why website design really matters in today's demanding market.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

744 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now