SQL:嵌套的选择语句-子选择从主选择语句读取var

时间:2019-01-20 19:24:26

标签: sql sql-server stored-procedures

我有3个表,分别为OrderTblOrderDetailTblProductTbl

OrderTbl 列包括以下内容:

OrderId | EventDate | etc...

OrderDetailTbl 列包括以下内容:

ProductId(fk) | RequiredQuantity | OrderId(fk) | etc...

ProductTbl 列包括以下内容:

ProductId | ProductName | TotalQuantityInStock

我的目标是创建一个存储过程,该存储过程将由我的应用程序调用以查看数据表中的数据,该数据表将合并三个像这样的表中的数据

ProductName | Total Quantity | EventDate | TotalRequiredQuantity
Product1    |  100           |  1-1-2019 | 20
Product2    |  200           |  1-1-2019 | 55
Product1    |  100           |  2-1-2019 | 60

我创建了如下语句:-

IF OBJECT_ID('tempdb..#temptbl', 'U') IS NOT NULL
    /*Then it exists*/
    DROP TABLE #temptbl

SELECT DISTINCT
    p.ProductName, p.TotalQuantity, o.OrderEventDate, 
    (SELECT SUM(od.OrderDetailReqQuant)
     FROM OrderDetailTbl AS od
     INNER JOIN OrderTbl AS o ON od.OrderId = o.OrderId
     WHERE o.OrderEventDate = '2019-01-18' 
       AND od.ProductId = 1002) AS TotalRequiredQuantity
INTO
    #temptbl
FROM
    ProductTbl AS p
INNER JOIN
    OrderDetailTbl AS od ON p.ProductID = od.ProductId
INNER JOIN
    OrderTbl AS o ON o.OrderId = od.OrderId

SELECT * FROM #temptbl

该语句产生以下结果:

ProductName    | TotalQuantity | OrderEventDate | TotalRequiredQuantity
---------------+---------------+----------------+----------------------
8' Tabel       |       50      | 2019-01-18     |     12
Banquet Chairs |      400      | 2019-01-17     |     12
Banquet Chairs |      400      | 2019-01-18     |     12
White Chairs   |      220      | 2019-01-18     |     12 

除了TotalRequiredQuantity列以外的所有数据都是正确的,因为您可以看到传递给该语句的数据是静态的,我的问题是:

如何将主选择语句中的当前p.ProductName和o.OrderEventDate传递给子选择语句?对于od.ProductId,我知道我必须运行另一个select语句,该语句将获取与p.ProductName对应的productId,但这对我来说不是问题。

感谢偷窥

3 个答案:

答案 0 :(得分:1)

我不太确定您要使用<div class="outer"> <div class="right"></div> <h2>A Heading</h2> <div class="pair"> <div>This is a box</div> <div>This is a wide box</div> </div> </div>过滤器做什么,所以我将给出3条建议:

首先,必须从子查询中删除内部联接。

第一个建议(将过滤条件保留在子查询中)

o.OrderEventDate = '2019-01-18' and od.ProductId = 1002

第二条建议(将过滤器移至原始查询)

SELECT DISTINCT p.ProductName, p.TotalQuantity, o.OrderEventDate, (select SUM(od.OrderDetailReqQuant)
        FROM OrderDetailTbl as od
        WHERE od.OrderId = o.OrderId and o.OrderEventDate = '2019-01-18' and od.ProductId = 1002)  as TotalRequiredQuantity
INTO #temptbl
FROM ProductTbl as p
INNER JOIN OrderDetailTbl as od ON p.ProductID = od.ProductId
INNER JOIN OrderTbl as o ON o.OrderId = od.OrderId

第三条建议(删除过滤器)

SELECT DISTINCT p.ProductName, p.TotalQuantity, o.OrderEventDate, (select SUM(od.OrderDetailReqQuant)
        FROM OrderDetailTbl as od
        WHERE od.OrderId = o.OrderId)  as TotalRequiredQuantity
INTO #temptbl
FROM ProductTbl as p
INNER JOIN OrderDetailTbl as od ON p.ProductID = od.ProductId
INNER JOIN OrderTbl as o ON o.OrderId = od.OrderId
WHERE o.OrderEventDate = '2019-01-18' and od.ProductId = 1002

答案 1 :(得分:0)

只需通过以下方式删除此内部制成的列

       (select SUM(od.OrderDetailReqQuant)
        from OrderDetailTbl as od
        inner join OrderTbl as o
            on od.OrderId = o.OrderId
                  where o.OrderEventDate = '2019-01-18' and 
              od.ProductId = 1002)....

并将此附加条件o.OrderEventDate = '2019-01-18' and od.ProductId = 1002添加到where子句本身

答案 2 :(得分:0)

在处理该语句之后,这是对我有用的最终结果:-

IF OBJECT_ID('tempdb..#temptbl', 'U') IS NOT NULL
/*Then it exists*/
DROP TABLE #temptbl

declare @totalreq int

select distinct p1.ProductName, p1.TotalQuantity, o1.OrderEventDate, (select SUM(od2.OrderDetailReqQuant)
                from OrderDetailTbl as od2
                inner join OrderTbl as o2
                    on od2.OrderId = o2.OrderId
                where o2.OrderEventDate = o1.OrderEventDate and od2.ProductId = 

                (select p3.ProductId from ProductTbl as p3 where p3.ProductName = p1.ProductName))  as TotalRequiredQuantity
into #temptbl
 from ProductTbl as p1
 inner join OrderDetailTbl as od1
    on p1.ProductID = od1.ProductId
 inner join OrderTbl as o1
    on o1.OrderId = od1.OrderId

    select * from #temptbl

我所做的一切都是在select语句的每个级别上定义一个重用表的唯一名称,因此可以说在主select语句中,我将producttbl定义为p1,在第一个子查询中,我再次将producttbl定义为p2,并在在第二个子查询中,我将producttbl定义为p3,因此现在我可以比较来自语句的3个级别的值并正确显示结果。结果如下:-

ProductName    |TotalQuantity|OrderEventDate|TotalRequiredQuantity
__________________________________________________________________________
8' Tabel       | 50          | 2019-01-18   |14
Banquet Chairs |400          | 2019-01-17   |2
Banquet Chairs |400          | 2019-01-18   |12
White Chairs   |220          | 2019-01-18   |5