If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Conditionnal DESC/ASC

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-22-09, 09:28
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Post Conditionnal DESC/ASC

Hello,

I'd like to condition the sort order of a query.
Let's give an example:
Code:
SET condition = TRUE;
SELECT id, name
FROM table_1
ORDER BY name IF(condition, DESC, ASC)
For sure this doesn't work.
Could someone tell me how can I do that ?
Is there an EVAL function which allows to do that ?
Like
Code:
SELECT id, name
FROM table_1
ORDER BY name EVAL(IF(condition, "DESC", "ASC"))
(I'm in a procedure)
Reply With Quote
  #2 (permalink)  
Old 01-22-09, 10:31
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
We don't order by expressions, only derived columns (by name or index).

The solution you're looking for is something like this
Code:
SELECT id
     , name
     , CASE WHEN condition = 'ASC'  THEN name END As [sort_asc]
     , CASE WHEN condition = 'DESC' THEN name END As [sort_desc]
FROM   your_table
ORDER
    BY sort_asc  ASC
     , sort_desc DESC

SELECT id
     , name
FROM   (
        SELECT id
             , name
             , CASE WHEN condition = 'ASC'  THEN name END As [sort_asc]
             , CASE WHEN condition = 'DESC' THEN name END As [sort_desc]
        FROM   your_table
       ) x
ORDER
    BY sort_asc  ASC
     , sort_desc DESC
__________________
George
Twitter | Blog
Reply With Quote
  #3 (permalink)  
Old 01-22-09, 11:54
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Quote:
We don't order by expressions
You mean you don't use to do that or it's not doable ?
Because actually this
Code:
SELECT id, name FROM table ORDER BY IF(TRUE, id, name)
works
Reply With Quote
  #4 (permalink)  
Old 01-23-09, 04:03
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Finally I'm going to make a view,
so it will be more maintainable.
Reply With Quote
  #5 (permalink)  
Old 01-23-09, 04:36
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
As you've noticed, it is possible to order by an expression. It's also possible for me to take a bath with the toaster, doesn't mean I want to do it!

Essentially the SQL standards state that you should order order by derived values, by their name/alias or their index (first column = 1, second column = 2, etc) - which is why we don't do it.

...and just to sound like (more of) a bore - you can't create views with an ORDER BY clause!


You can easily create the view without that bit, and then when you select from the view add the order by clause; e.g.
Code:
SELECT id
     , name
FROM   your_view
ORDER
    BY sort_asc  ASC
     , sort_desc DESC
__________________
George
Twitter | Blog
Reply With Quote
  #6 (permalink)  
Old 01-23-09, 04:49
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Yes, that's what I wanted to do.
Reply With Quote
  #7 (permalink)  
Old 01-23-09, 05:58
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by georgev
We don't order by expressions, only derived columns (by name or index).
Why is that? Is it a performance thing? Ordering of data is most likely to be done on data that is about to be displayed to the user. The amount of data to be displayed would normally be quite small (less than 100 records, preferably less than 20 records) otherwise the user will just ignore the report and use it instead as a paper weight. I see how you order the data to be more a business requirement rather than a performance requirement so if the business wanted the data to be displayed in order of field x that's exactly what they'll get - most likely they'll want the ability to order by just about every field that's on the display.
Quote:
Originally Posted by gtk
Finally I'm going to make a view,
so it will be more maintainable.
If you're doing this with a stored proc you could just do it this way which might be slightly more easier to read (ie more maintainable) :
Code:
SET condition = TRUE;

if condition then
   SELECT id, name
   FROM table_1
   ORDER BY name
else
   SELECT id, name
   FROM table_1
   ORDER BY name DESC
end if;
Other options to consider if you're outputting the data to some report via an external program such as PHP might be to just build the final select statement in PHP depending on your condition.
Code:
$condition = TRUE;

if ( $condition ) {
   $sql = "SELECT id, name FROM table_1 ORDER BY name"
} else {
   $sql = "SELECT id, name FROM table_1 ORDER BY name desc"
}
Of course if you were using a numeric value to order by then it would be easy enough:
Code:
SET condition = TRUE;

SELECT id, name
FROM table_1
ORDER BY IF( condition , val , val * -1)
Mike
Reply With Quote
  #8 (permalink)  
Old 01-23-09, 06:37
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
Quote:
Originally Posted by mike_bike_kite
Why is that?
It's a standards thing
Quote:
Originally Posted by georgev
Essentially the SQL standards state that you should order order by derived values, by their name/alias or their index (first column = 1, second column = 2, etc) - which is why we don't do it.
As for performance - if you think about how a query executes, the very last step is generally the order by, right? Which means an expression would have to be re-evaluated again.

...in theory - this is one I've not proved yet but I could do if anyone is interested enough (proof would be SQL Server )
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 01-23-09, 07:14
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by georgev
It's a standards thing
Who's standards? So you're saying that if the business wants to be able to order it's report by any field on screen then you would simply say no and tell them you can't because "it's a standards thing"???? You're braver than me!
Quote:
Originally Posted by georgev
As for performance - if you think about how a query executes, the very last step is generally the order by, right? Which means an expression would have to be re-evaluated again.
So what? that's hardly a reason to refuse to do it. If the requirement is for a report to ordered by an expression then that's what you provide. If you can suggest a better way of doing things then great but if I'd asked for a certain ordering and you said you wouldn't provide it because it means an expression would have to be re-evaluated again then I'd be having a quite word with your boss.
Reply With Quote
  #10 (permalink)  
Old 01-23-09, 07:41
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
ANSI / ISO standards (sorry for not being clearer earlier)
__________________
George
Twitter | Blog
Reply With Quote
  #11 (permalink)  
Old 01-23-09, 07:44
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
As for examples of how to remove the expression from the order by - see post 2 - it contains two variations of this theme.
__________________
George
Twitter | Blog
Reply With Quote
  #12 (permalink)  
Old 01-23-09, 08:21
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by georgev
ANSI / ISO standards ...
As for examples of how to remove the expression from the order by - see post 2 - it contains two variations of this theme.
I think we'll have to agree to differ on what's more maintainable then. To me example 2b looks like a typical NZDF solution to ordering and the more fields that are candidates for ordering then the more complex that query becomes. I'm guessing that in the least complex case then your example might be marginally faster but I doubt if you'd be able to measure the difference in real time. I certainly know which example I'd use if it was me that had to maintain the code.

Perhaps it's just me seeing things differently to the rest of the civilised world ... again.
Reply With Quote
  #13 (permalink)  
Old 01-23-09, 08:26
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
You appear to have overlooked the subtlety in the second example...

The first query returns the derived columns as part of the resultset while the second example does not. Both examples are ordered correctly based on the a parameter setting.
__________________
George
Twitter | Blog
Reply With Quote
  #14 (permalink)  
Old 01-23-09, 09:26
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Quote:
Originally Posted by georgev
You appear to have overlooked the subtlety in the second example...
I think the fact it was too subtle for me indicates it might be a maintenance headache in future

I know my example used a choice of two selects rather a single complex one but the example below is pretty quick to understand and would work in a sproc :
Code:
if condition then
   SELECT id, name
   FROM table_1
   ORDER BY name
else
   SELECT id, name
   FROM table_1
   ORDER BY name DESC
end if;
Reply With Quote
  #15 (permalink)  
Old 01-23-09, 09:43
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I like that suggestion too Mike. It will be the best performer of the lot because it only has a single order by (and no computations either).
__________________
George
Twitter | Blog
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On