Hey everyone,
I am currently trying to combine two case statements.
I have written this statement:
CASE
WHEN DATEDIFF(`actual_ship_date`,`release_date`) < 2 THEN 'A'
WHEN DATEDIFF(`actual_ship_date`,`release_date`) <= 3 THEN 'B'
WHEN DATEDIFF(`actual_ship_date`,`release_date`) <= 4 THEN 'C'
When DATEDIFF(`actual_ship_date`,`release_date`) <= 5 THEN 'D'
When DATEDIFF(`actual_ship_date`,`release_date`) > 5 Then 'F'
End
I am using this statement to give the warehouses a grade depending on how quickly they ship out a given order. But the problem with this is that it does not remove the weekend (our warehouse is closed on saturday and Sunday). I would like it to remove the weekend and still give each warehouse the appropriate grade with the weekends not accounted for.
I have this statement now to remove the weekends, I am trying to comine these two statements so that it will still remove the weekend and give each warehouse a grade:
AVG(CASE WHEN `release_date` = `actual_ship_date` THEN 0 ELSE DATEDIFF(
CASE -- move end date to friday if on weekend
WHEN dayofweek(`actual_ship_date`) = 1 THEN SUBDATE(`actual_ship_date`, INTERVAL 1 DAY)
ELSE `actual_ship_date`
END
,
CASE -- move start date to monday if on weekend
WHEN dayofweek(`release_date`) = 7 THEN ADDDATE(`release_date`, INTERVAL 2 DAY)
WHEN dayofweek(`release_date`) = 1 THEN ADDDATE(`release_date`, INTERVAL 1 DAY)
ELSE `release_date`
END)
-
(-- finds number of weeks and then times by 2 to get number of weekend days
FLOOR((DATEDIFF(
CASE -- move end date to friday if on weekend
WHEN dayofweek(`actual_ship_date`) = 7 THEN SUBDATE(`actual_ship_date`, INTERVAL 1 DAY)
WHEN dayofweek(`actual_ship_date`) = 1 THEN SUBDATE(`actual_ship_date`, INTERVAL 2 DAY)
ELSE `actual_ship_date`
END,
CASE -- move start date to monday if on weekend
WHEN dayofweek(`release_date`) = 7 THEN ADDDATE(`release_date`, INTERVAL 1 DAY)
WHEN dayofweek(`release_date`) = 2 THEN SUBDATE(`release_date`, INTERVAL 1 DAY)
WHEN dayofweek(`release_date`) = 3 THEN SUBDATE(`release_date`, INTERVAL 2 DAY)
WHEN dayofweek(`release_date`) = 4 THEN SUBDATE(`release_date`, INTERVAL 3 DAY)
WHEN dayofweek(`release_date`) = 5 THEN SUBDATE(`release_date`, INTERVAL 4 DAY)
WHEN dayofweek(`release_date`) = 6 THEN SUBDATE(`release_date`, INTERVAL 5 DAY)
ELSE `release_date`
END
) / 7)) * 2)
END)
I have attatched a image of the card i am making. It is currently set up only using the first case statement that I listed and is doing exactly what I want it to do besides accomodate for the weekend. That is why I would like to combine these two statements to make it work. Does anyone have any advice on how to go about this?