Complicated Order By Clause?

Posted by Todd on Stack Overflow See other posts from Stack Overflow or by Todd
Published on 2010-03-26T15:06:08Z Indexed on 2010/03/26 15:33 UTC
Read the original article Hit count: 196

Filed under:
|

Hi. I need to do what to me is an advanced sort. I have this two tables:

Table: Fruit  

fruitid | received | basketid  
  1       20100310   2  
  2       20091205   3  
  3       20100220   1  
  4       20091129   2  

Table: Basket  
id | name  
1    Big Discounts  
2    Premium Fruit  
3    Standard Produce  

I'm not even sure I can plainly state how I want to sort (which is probably a big part of the reason I can't seem to write code to do it, lol).

I do a join query and need to sort so everything is organized by basketid. The basketid that has the oldest fruit.received date comes first, then the other rows with the same basketid by date asc, then the basketid with the next earliest fruit.received date followed by the other rows with the same basketid, and so on.

So the output would look like this:

Fruitid | Received  |   Basket  
   4      20091129      Premuim Fruit  
   1      20100310      Premuim Fruit  
   2      20091205      Standard Produce  
   3      20100220      Big Discounts  

Any ideas how to accomplish this in a single execution?

© Stack Overflow or respective owner

Related posts about sql

Related posts about order