SDDOCO, SDDCTO, SDLNID, SDTRDJ, SDDRQJ, SDADDJ, SDCO, SDMCU, SDUORG, SDUOM, SDPQOR, SDUOM1, SDSQOR,
SDUOM2, SDITWT, SDUOM4, SDUPRC, SDAEXP, SDLTTR, SDNXTR, SDURRF, SDCDCD, SHVR01, SDTORG, SHHOLD, SDCNID,
SDAN8, SDSHAN, SDLITM
FROM TEST8DTA.F42119@LGKDDB A
LEFT OUTER JOIN TEST8DTA.F42019@LGKDDB B ON A.SDDOCO = B.SHDOCO AND A.SDDCTO = B.SHDCTO
WHERE SDTRDJ BETWEEN 111213 AND 111251 AND
SDDCTO NOT IN ('SA','SW','SH','ST','S5','SK','CA','CW') AND SDLTTR <> '980'
) sales ON add_bk.ABAN8 = sales.SDAN8
INNER JOIN TEST8DTA.F0301@LGKDDB cust_mst ON add_bk.ABAN8 = cust_mst.A5AN8 AND LENGTH(add_bk.ABAN8)=4 -- customer master
LEFT OUTER JOIN TEST8DTA.F0101@LGKDDB C ON cust_mst.A5CMC1 = C.ABAN8 -- emp number
LEFT OUTER JOIN TEST8DTA.F0101@LGKDDB D ON cust_mst.A5CMC2 = D.ABAN8 -- dept code
LEFT OUTER JOIN TEST8DTA.F0101@LGKDDB F ON Trim(sales.SDMCU) = F.ABAN8 -- plant code
LEFT OUTER JOIN TEST8DTA.F0101@LGKDDB sh ON sales.sdshan = sh.ABAN8 AND Length(SH.ABAN8) = 7-- ship to code
LEFT OUTER JOIN TEST8DTA.F0116@LGKDDB addres ON sh.ABAN8 = addres.ALAN8 -- address
LEFT OUTER JOIN TEST8DTA.F4006@LGKDDB addres ON sales.sddoco = addres.oadoco AND sales.sddcto = addres.oadcto AND OAANTY = '2'
INNER JOIN TEST8DTA.F4101@LGKDDB itm ON sales.sdlitm = itm.imlitm AND IMSTKT <> 'O' -- item master
AND IMGLPT IN ('LK20','LK21','LK22','LK10','LK11','LK12')
AND IMSRP1 IN ('A','B','D','F','MP')
LEFT OUTER JOIN TEST8CTL.F0005@LGKDDB btype ON Trim(add_bk.ABAC06) = Trim(btype.DRKY) AND btype.DRSY='01' and btype.DRRT='06' -- bILDING TYPE