In the Subquery, I would like to be able to add in the WHERE clause:
duedate<=quotehed.duedate
quotehed.duedate is in the main query. I don’t know how to bring the quotehed.duedate into the subquery because it is not in the same table as my subquery. I also don’t know how to make sure the quotehed.duedate will pull in the due date from the quote number that is selected from the parameter @p_quotenum.
My enitre query follows below. I greatly appreciate any help! Thanks!
SELECT partwhse.warehousecode,
partwhse.allocqty,
partwhse.onhandqty,
quotehed.quotenum,
quotehed.custnum,
quotehed.datequoted,
quotehed.duedate,
quotedtl.quoteline,
quotedtl.partnum,
quotedtl.reqshipdate,
quotedtl.sellingexpectedqty,
plantwhse.plant,
part.partdescription,
t_partdtl1.totaldemand
FROM part
INNER JOIN quotedtl
ON part.company = quotedtl.company
AND part.partnum = quotedtl.partnum
LEFT OUTER JOIN (SELECT company,
partnum,
requirementflag,
SUM(quantity) AS totaldemand,
plant,
duedate
FROM partdtl AS partdtl_1
WHERE ( company = 'lot' )
AND ( requirementflag = '1' )
AND ( plant = @p_plant )
GROUP BY company,
partnum,
requirementflag,
plant,
duedate) AS t_partdtl1
ON quotedtl.company = t_partdtl1.company
AND quotedtl.partnum = t_partdtl1.partnum
LEFT OUTER JOIN partwhse
INNER JOIN plantwhse
ON partwhse.company = plantwhse.company
AND partwhse.partnum = plantwhse.partnum
AND partwhse.warehousecode = plantwhse.warehousecode
ON quotedtl.company = plantwhse.company
AND quotedtl.partnum = plantwhse.partnum
RIGHT OUTER JOIN quotehed
ON quotedtl.company = quotehed.company
AND quotedtl.quotenum = quotehed.quotenum
WHERE ( quotehed.quotenum = @p_quotenum )
AND ( quotehed.company = 'lot' )
AND ( plantwhse.plant = @p_plant )
ORDER BY quotehed.quotenum,
quotedtl.quoteline
You should try to reorder your joins to place the subquery after you join
quotehed. Once that is done then join the subquery to thequotehedtable using the date filter, similar to this: