Solved

MYSQL Conditional Order By with Union

Posted on 2009-07-13
4
622 Views
Last Modified: 2012-05-07
I have a table that has BayNumber, ShelfNumber and ShelfPosition.  When the BayNumber is odd it needs to sort that grouping one way, when the BayNumber is even it needs to sort that grouping another way.

I have tried to accomplish this by using a UNION ALL and having one query return all the EVEN rows and another all the ODD, sorting them correctly in each individual SELECT statement.  I have confirmed that each SELECT is working as intended.

When I mash the two together and attempt to do a GLOBAL order by the BayNumber it does the global but the individual query order is not maintained.  Here is the gist of the query to help.

I have tried some CASE statements in the global order but that isn't respecting the order of the individual.

It should return the following when correct

Bay | Shelf | Position
1 | A | 2
1 | A | 4
2 | A | 15
2 | A | 2

The Bay number needs to drive the Position order.

Hopefully that makes sense to all you experts, I am by far not a SQL expert and this one has me stumped.
(SELECT *, MOD(BayNumber,2) as EvenOdd
FROM orderdetails a
WHERE a.prodtype != 'P'
GROUP BY detailsId, detailproductid, detailoption1 
HAVING EvenOdd=0
ORDER BY BayNumber, ShelfNumber, ShelfPosition ASC
)
 
 
UNION ALL
 
(SELECT *, MOD(BayNumber,2) as EvenOdd
FROM orderdetails a
WHERE a.prodtype != 'P'
GROUP BY detailsId, detailproductid, detailoption1 
HAVING EvenOdd=1
ORDER BY BayNumber, ShelfNumber, ShelfPosition DESC
)
 
ORDER BY BayNumber

Open in new window

0
Comment
Question by:pmessana
[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
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24843193
>It should return the following when correct
based no what input data?

your SQL is a bit violating some sql rules, "unfortunately" mysql works a bit differently that other sql databases...
0
 
LVL 14

Accepted Solution

by:
racek earned 500 total points
ID: 24843496

SELECT a.*, MOD(BayNumber,2) as EvenOdd
FROM orderdetails a
WHERE a.prodtype != 'P'
GROUP BY detailsId, detailproductid, detailoption1 
HAVING EvenOdd in(0,1)
ORDER BY BayNumber, ShelfNumber, EvenOdd, 
         IF(EvenOdd = 0,  ShelfPosition * -1, ShelfPosition ) DESC

Open in new window

0
 
LVL 9

Author Comment

by:pmessana
ID: 24843723
Angellll I cut some of the query out.  The point is, there are three columns, BayNumber, ShelfNumber, ShelfPosition.

Racek - brilliant!  I didn't think to turn it negative and keep the same sort going, you got rid of my Union!!!
0
 
LVL 9

Author Closing Comment

by:pmessana
ID: 31602982
Awesome answer, so much easier than what I was trying.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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