WITH orders AS (
	SELECT OrderID
    FROM custopayted.orders
    WHERE orderdate > DATE_ADD(DATE_FORMAT(NOW() ,'%Y-%m-01'), INTERVAL -1 MONTH) AND orderdate < DATE_FORMAT(NOW() ,'%Y-%m-01')
)

SELECT 
    vm.MFG_SKU
FROM orders o
JOIN custopayted.orderdetails od ON o.orderid = od.orderid
JOIN products.pins p ON od.productcode = p.productcode
JOIN products.vendor_map vm ON p.productcode = vm.productcode AND vm.VendorCode = 'UED'
WHERE NOT (
    (od.productnote NOT LIKE '% | %' AND od.productnote LIKE 'UED %')
    OR od.productnote LIKE '% | UED %'
)
AND od.productnote NOT LIKE '%STOCK%'
AND od.productnote NOT LIKE '%(C)%'
AND od.productnote NOT LIKE '%FNLA%'
AND od.productnote NOT LIKE '%LTBO%'
GROUP BY vm.MFG_SKU, vm.Cost
ORDER BY vm.MFG_SKU
LIMIT 500