0

在子查询中,我希望能够在 WHERE 子句中添加:

duedate<=quotehed.duedate  

quotehed.duedate 在主查询中。我不知道如何将 quotehed.duedate 带入子查询,因为它与我的子查询不在同一个表中。我也不知道如何确保 quotehed.duedate 将从参数中选择的报价单中提取截止日期@p_quotenum

我的整个查询如下。我非常感谢任何帮助!谢谢!

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 
4

1 回答 1

3

您应该尝试重新排序您的连接,以便在您加入后放置子查询quotehed。完成后,使用日期过滤器将子查询连接到quotehed表,类似于:

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 JOIN plantwhse
    ON quotedtl.company = plantwhse.company 
    AND quotedtl.partnum = plantwhse.partnum 
LEFT OUTER JOIN partwhse
    ON partwhse.company = plantwhse.company 
    AND partwhse.partnum = plantwhse.partnum 
    AND partwhse.warehousecode = plantwhse.warehousecode 
RIGHT OUTER JOIN quotehed
    ON quotedtl.company = quotehed.company 
    AND quotedtl.quotenum = quotehed.quotenum
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 
    AND t_partdtl1.duedate < quotehed.duedate
WHERE (quotehed.quotenum = @p_quotenum) 
    AND (quotehed.company = 'lot') 
    AND (plantwhse.plant = @p_plant)
ORDER BY quotehed.quotenum, quotedtl.quoteline
于 2013-01-16T21:30:45.647 回答