?
Solved

Special Sorting

Posted on 2014-01-02
5
Medium Priority
?
249 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 84

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 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

764 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