----------
SQL
----------
(simplified to make it easier to read)
SELECT ijtd_export.cart.Customer_URN
ijtd_export.customer.Customer_Title
ijtd_export.customer.Customer_Forename
ijtd_export.customer.Customer_Surname
ijtd_export.customer.Customer_Telephone
max(ijtd_export.cart.Cart_Order_DateTime)
max(outcall.callnotes.CreatedDate)
outcall.callnotes.CallBackStatus
FROM ijtd_export.cart
JOIN ijtd_export.cartitem ON ijtd_export.cartitem.Cart_URN = ijtd_export.cart.Cart_URN
JOIN ijtd_export.customer ON ijtd_export.cart.Customer_URN = ijtd_export.customer.Customer_URN
JOIN ijtd_export.product ON ijtd_export.product.Product_URN = ijtd_export.cartitem.Product_URN
LEFT JOIN outcall.callnotes ON outcall.callnotes.Customer_URN = ijtd_export.customer.Customer_URN
WHERE
cart.Cart_Order_DateTime BETWEEN '"&IncStartDate&"' AND '"&IncEndDate&"' AND
cart.Cart_Status = 'Order' AND
product.Product_Code IN '"&SPartCodes&"' AND
cart.Cart_Order_Group = '1' AND
NOT Customer_Surname LIKE '' AND
NOT Customer_Marketing LIKE 'DEAD' AND
NOT Customer_Telephone_Prefs LIKE 'Do not call'
|