FROM子句中包含多个表的子查询

时间:2013-01-16 21:18:26

标签: tsql subquery

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

duedate<=quotehed.duedate  

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

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

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 

1 个答案:

答案 0 :(得分: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