在条件的地方添加计算

时间:2014-02-13 11:10:37

标签: sql-server-2008-r2

我有以下代码

Select MR.MRPRoductId,MR.PartNo, E.MaterialName as Producttomake,EP.MaterialName as NeededProduct,(MR.ApprovedQty*PCM.NeededQuantity-isnull(SUM(ISS.IssuedQty),0))as Needed,PCM.NeededProductPartNo,
EP.UOM,ISNULL(SM.Quantity,0)as Available from Material_Requisition MR 
join Productconversion_master PCM on MR.PartNo=PCM.ConvertedProductPartNo 
Left join ProductConversion_IssueSlip ISS on PCM.NeededProductPartNo=ISS.IssuedPartNo
join ElectricalProducts E on MR.PartNo=E.PartNo join ElectricalProducts EP on PCM.NeededProductPartNo=EP.PartNo 
Left join StockMaster SM  on PCM.NeededProductPartNo=SM.PartNo and SM.LocationId='LOC_001' where MR.MRID='TFIPL/MR/CON/NR/2013-14/00001' 
 group by EP.MaterialName,MR.MRPRoductId,MR.PartNo,E.MaterialName,
MR.ApprovedQty,PCM.NeededQuantity,PCM.NeededProductPartNo,EP.UOM,SM.Quantity

此查询的结果如下

Result

我想只选择Needed> 0 .how将其添加到where where

1 个答案:

答案 0 :(得分:1)

两个选项。

选项1 - 我认为最简单

SELECT list
     , of
     , columns
FROM   (
        <your query goes here>
       ) As a_subquery
WHERE  needed > 0

选项2

<your_query goes here>
HAVING (MR.ApprovedQty*PCM.NeededQuantity-isnull(SUM(ISS.IssuedQty),0)) > 0

您不能在此方案中使用WHERE条件,因为它适用于聚合。与聚合相当的是HAVING子句(在 GROUP BY之后放置