Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL order by 2 date columns

Tags:

sql

I have a table with orders. Each order has a delivery_date, and a collection_date.

I want to retrieve the orders as a schedule for a week which shows what the next thing is to do.

So I guess I want the time sequence shown so that a collection on Wednesday appears in between a delivery on Tuesday and another delivery on Thursday.

Any ideas on how to do this with SQL?

like image 547
Spunog Avatar asked Dec 05 '22 16:12

Spunog


1 Answers

Using a Union ALL, I'm treating each of your order records as two records: one for a delivery and one for a collection. If you had another date (repair?), you would union another sub query with similar fields.

I made up a lot of this since you did not provide any specs on your order table.

select *
from (
   Select OrderID
       , 'Colection' as VisitType
      , Collection_Date as VisitDate
   from Orders
   Union All
   Select OrderID
     , 'Delivery' as VisitType
     , Delivery_Date as VisitDate
  from Orders
) as v
order by v.VisitDate
like image 146
JeffO Avatar answered Dec 07 '22 04:12

JeffO