Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding business days to SQL query

Tags:

sql

php

mysql

I've look almost everywhere to find this. I have the following SQL statement:

SELECT COUNT(*) FROM `job_sheet`
LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
WHERE job_sheet.completion=".ORDER_COMPLETE."
AND deliveries.ship_date>job_sheet.delivery_date

Which basically tells me which completed jobs were shipped past their delivery_date (DUE DATE). However, the true delivery date is in reality 7 business days (holidays don't matter. basically just skipping the weekend) ahead than what's in the database.

What I need to be able to do is add 7 days to this section: deliveries.ship_date>job_sheet.delivery_date + 7 business days

My first thought was to use DATEADD() function, however It's rare that I use it and it's quite confusing on how to implement it in this situation.

The date is stored as: YYYY-MM-DD

like image 430
Dimitri Avatar asked Oct 06 '22 13:10

Dimitri


1 Answers

try this

   SELECT COUNT(*) FROM `job_sheet`
   LEFT JOIN deliveries ON job_sheet.job_id=deliveries.jID
   WHERE job_sheet.completion=".ORDER_COMPLETE."
   AND  deliveries.ship_date> DATE_ADD(`job_deliveries.ship_date` , INTERVAL 7 DAY) 

REFRENCE OF ADD_DATE

like image 110
echo_Me Avatar answered Oct 09 '22 20:10

echo_Me