在Sql Server中使用单个搜索条件搜索多个表

时间:2016-01-13 01:39:04

标签: sql-server

我需要帮助。我有这个SQL语句:

ALTER proc [dbo].[GetDSR]
(
    @WholeSellerId varchar(50),
    @FromDate date
)
as
begin
with cte1 as
    (select co.ProductId as copid, co.DateAdded as coda, sum(isnull(co.quantity,0)) as coq, co.WholeSellerId as coid
    from ConsignmentSale co 
    group by co.ProductId, co.DateAdded, co.WholeSellerId),

cte2 as
(select ca.ProductId as caid, sum(isnull(ca.quantity,0)) as caq from CashSale ca
group by ca.ProductId),

cte3 as
(select wi.ProductId as wiid, sum(isnull(wi.Quantity,0)) as wiq from Withdrawal wi
group by wi.ProductId),

cte4 as
(select po.ProductId as poid, sum(isnull(po.Quantity,0)) as poq from Pullout po
group by po.ProductId),

cte5 as 
(select pr.ProductId as prid, sum(isnull(pr.Quantity,0)) as prq from Promotion pr
group by pr.ProductId)

select cte1.copid as Product, cte1.coda, isnull(cte1.coq,0) as Credit, isnull(cte2.caq,0) as Cash, 
isnull(cte3.wiq,0) as Withdrawal, isnull(cte4.poq,0) as Pullout, isnull(cte5.prq,0) as Promotion

from cte1
full outer join cte2 on cte2.caid=cte1.copid
full outer join cte3 on cte3.wiid=cte1.copid
full outer join cte4 on cte4.poid=cte1.copid
full outer join cte5 on cte5.prid=cte1.copid
where cte1.coid = @WholeSellerId and cte1.coda = @FromDate
end
go

只有当ConsignmentSale中的数据符合特定条件时,才会给出正确的结果。问题是,当ConsignmentSale中没有满足条件的数据但是在其他表中时,它不会显示。可能是因为这部分:

where cte1.coid = @WholeSellerId and cte1.coda = @FromDate

它只使用单个表中的条件ConsignmentSale,并且它不会检查表的其余部分是否符合此条件。请告诉我如何改变这个程序,即使在ConsignmentSale中没有达到标准,但在另一个表中它被满足,它仍然会给我结果,而coq只会是0,因为它确实如此没有符合该标准的数据。 谢谢。

2 个答案:

答案 0 :(得分:0)

将过滤器移至ON条件

................
FROM   cte1 
   FULL OUTER JOIN cte2 
                ON cte2.caid = cte1.copid 
                   AND cte1.coid = @WholeSellerId 
                   AND cte1.coda = @FromDate 
   FULL OUTER JOIN cte3 
                ON cte3.wiid = cte1.copid 
   FULL OUTER JOIN cte4 
                ON cte4.poid = cte1.copid 
   FULL OUTER JOIN cte5 
                ON cte5.prid = cte1.copid 
.................

答案 1 :(得分:0)

我刚刚在每个语句上创建了一个标准,如下所示:

    ALTER proc [dbo].[GetDSR]
(
    @WholeSellerId varchar(50),
    @FromDate date
)
as
begin
with 
cte1 as
    (select pr.ProductId from Product pr),

cte2 as 
    (select co.WholeSellerId as cowi, co.DateAdded as coda, co.ProductId as coid, sum(isnull(co.Quantity,0)) as coq from ConsignmentSale co
    where co.WholeSellerId=@WholeSellerId and co.DateAdded=@FromDate
    group by co.ProductId, co.DateAdded, co.WholeSellerId),

cte3 as
    (select ca.DateAdded as cada, ca.WholeSellerId as cawi, ca.ProductId as caid, sum(isnull(ca.Quantity,0)) as caq from CashSale ca
    where ca.WholeSellerId=@WholeSellerId and ca.DateAdded=@FromDate
    group by ca.ProductId, ca.WholeSellerId, ca.DateAdded),

cte4 as
    (select pm.DateAdded as pmda, pm.ProductId as pmid, pm.WholeSellerId as pmwi, sum(isnull(pm.Quantity,0)) as pmq from Promotion pm
    where pm.WholeSellerId=@WholeSellerId and pm.DateAdded=@FromDate
    group by pm.DateAdded, pm.ProductId, pm.WholeSellerId),

cte5 as 
    (select wi.ProductId as wiid, wi.DateAdded as wida, wi.WholeSellerId as wiwi, sum(isnull(wi.Quantity,0)) as wiq from Withdrawal wi
    where wi.WholeSellerId=@WholeSellerId and wi.DateAdded=@FromDate
    group by wi.DateAdded, wi.WholeSellerId, wi.ProductId),

cte6 as 
    (select po.ProductId as poid, po.DateAdded as poda, po.WholeSellerId as powi, sum(isnull(po.Quantity,0)) as poq from Pullout po
    where po.WholeSellerId=@WholeSellerId and po.DateAdded=@FromDate
    group by po.ProductId, po.DateAdded, po.WholeSellerId)

select cte1.ProductId as 'Product', isnull(cte2.coq,0)  as 'Credit', isnull(cte3.caq,0) as 'Cash', isnull(cte5.wiq,0) as 'Withdrawal', 
isnull(cte6.poq,0) as 'Pullout', isnull(cte4.pmq,0) as 'Promotion' 
from cte1
full outer join cte2 on cte2.coid = cte1.ProductId
full outer join cte3 on cte3.caid=cte1.ProductId
full outer join cte4 on cte4.pmid=cte1.ProductId
full outer join cte5 on cte5.wiid=cte1.ProductId
full outer join cte6 on cte6.poid=cte1.ProductId
end