非常感谢您的光临!
通常,此表在“检查呼叫”名称中将具有一个字段“ Pickup-Actual”,表示已被拾取。但是,在某些情况下,CheckCallName将绕过代答并转为交付状态。在这种情况下,我想以与使用实际提货相同的方式拉出查询以处理“已交付”状态。
因此,如果kpo.loadtracking在任何一个订单的“检查呼叫名称”字段中都没有“代答-实际”,我想拉日期(KeypointStatusDate),将状态更改为“已交付”。
我认为这类似于“其中Pickup-Actual的状态计数大于0,然后拉取最早的实际状态更新日期。
但是,当计数为0时,我希望它拉取与Delivered CheckCallName相关的日期。
表1应该拉10/5- 表2应该拉10/2
我当前的代码如下:
SELECT DISTINCT
Load.LoadNumber
,CustomerName
,OrderStatus
,PickedUpOn
,CheckCallName
,Min(LoadTracking.KeypointStatusDate) AS Earliest_Check_Call
,CustomerRate
,CarrierCost
,Miles
FROM kpo.load
INNER JOIN kpo.LoadExtension ON LOAD.LoadId = LoadExtension.LoadId
INNER JOIN kpo.Customer ON customer.CustomerId = LOAD.CustomerID
INNER JOIN kpo.LoadTracking ON LoadTracking.LoadId = LOAD.LoadId
WHERE
DATEDIFF(MONTH, GETDATE(), LoadTracking.KeypointStatusDate) = - 2
AND OrderStatus <> 'VOID'
AND CheckCallName = 'Pickup - Actual'
--- WE NEED TO THEN LOOK AT EACH LOAD
--- AND IF THERE IS NO PICKUP-ACTUAL THEN IT PULLS IN THE DELIVERED DATE.
--- THERE ARE SOME ORDERS THAT GO STRAIGHT TO DELIVERED STATUS. (453653)
GROUP BY
Load.LoadNumber
,OrderStatus
,CustomerName
,CheckCallName
,PickedUpOn
,CustomerRate
,CarrierCost
,Miles
ORDER BY
Earliest_Check_Call