Solved

Special Sorting

Posted on 2014-01-02
5
237 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
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 40

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

929 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

17 Experts available now in Live!

Get 1:1 Help Now