0

I have an sql query which shows the delivery details of a vehicle. ( it uses greatest to fetch max value from a range of colums for each vehicle stop)

SELECT deliveryid AS deliverynumber, loadid1 AS loadnumberdate, 
       haulieraccepted AS haulier, 
       greatest(drop1arrivedatetime, drop2arrivedatetime, drop3arrivedatetime, 
                drop4arrivedatetime, drop5arrivedatetime) AS planneddate, 
       date(greatest(ActualDrop1Arrive, ActualDrop2Arrive, ActualDrop3Arrive, 
                     ActualDrop4Arrive, ActualDrop5Arrive )) AS actualenddate,
       mitigation
    FROM deliverydetails
    WHERE deliveryid=44

the output is

deliverynumber | loadnumberdate | haulier         | planneddate         | actualenddate | mitigation 
44             | 484487         | stols transport | 2011-11-26 15:50:00 | 2011-11-26    | customerdelay

How can I add to the mysql query to compare columns 'planneddate' and 'actualenddate'? if the dates are the same then set the query field to 'ontime' else if actualenddate>planneddate then 'deliverylate'. So ideally I want the following output:

deliverynumber | loadnumberdate | haulier         | planneddate         | actualenddate | mitigation    | Status
44             | 484487         | stols transport | 2011-11-26 15:50:00 | 2011-11-26    | customerdelay | ontime.

Thanks for the assistance.

3 Answers 3

2

You can use a CASE statement or IF function. Perhaps something like:

SELECT ....,  IF(actualenddate>planneddate,'deliverylate','ontime') AS status FROM ....
Sign up to request clarification or add additional context in comments.

Comments

1

use mysql if condition and date conversion function to check and display according to....

Comments

1

You can wrap your original query as a subquery. This will rename the columns. Then, use a case ... then clause to add the column.

Assuming your original query works just fine, it would look like this:

select
    *,
    case when (... some comparison on 'planneddate' and 'actualenddate' ...) 
         then <true output> 
             else <false output> end
from
    (<your original query>) as myalias;

The trick is that the columns from the subquery are renamed, allowing you to use their new names (planneddate and actualenddate).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.