我正在运行SSMS 2008
select
A.JOBNUMBR as [Service Call], A.PONUMBER as [PO Number],
sum(A.QTYUNCMTBASE*A.UNITCOST) as [Committed Cost PO],
SUM(B.WS_Committed_Cost) as [Committed Cost WS],
(sum(A.QTYUNCMTBASE*A.UNITCOST)-SUM(B.WS_Committed_Cost)) as [Variance]
from
POP10110 A
LEFT JOIN
SV_Costs B on A.JOBNUMBR = B.Service_Call_ID and A.PONUMBER = B.Reference_TRX_Number and B.WS_Committed_Cost <> 0
where
A.Product_Indicator = 3
and A.QTYUNCMTBASE <> 0
and (sum(A.QTYUNCMTBASE * A.UNITCOST) - SUM(B.WS_Committed_Cost)) <> 0
group by
A.JOBNUMBR, A.PONUMBER
order by
A.JOBNUMBR, A.PONUMBER
运行此查询时,我在主题行中收到错误MSG 147。第3个where子句与我试图过滤到&lt;&gt;的聚合列[Variance]相同0.查询在没有第3个where子句的情况下运行。
我一直在做一些阅读并理解我需要在where子句中包含HAVING或select语句,但我不能正确地理解语法,任何帮助都会受到赞赏。
谢谢
塔尼亚
答案 0 :(得分:0)
Sum是一个聚合列,在您的选择(WHERE子句)中使用无效。一种解决方案是将其包装在另一个SELECT上。 OR:
SELECT
A.JOBNUMBR as [Service Call],
A.PONUMBER as [PO Number],
SUM(A.QTYUNCMTBASE*A.UNITCOST) as [Committed Cost PO],
SUM(B.WS_Committed_Cost) as [Committed Cost WS],
SUM(A.QTYUNCMTBASE*A.UNITCOST) - SUM(B.WS_Committed_Cost) as [Variance]
FROM POP10110 A
LEFT JOIN SV_Costs B
ON A.JOBNUMBR = B.Service_Call_ID
AND A.PONUMBER = B.Reference_TRX_Number
AND B.WS_Committed_Cost <> 0
WHERE A.Product_Indicator = 3
AND A.QTYUNCMTBASE <> 0
GROUP BY A.JOBNUMBR, A.PONUMBER
HAVING (SUM(A.QTYUNCMTBASE * A.UNITCOST) - SUM(B.WS_Committed_Cost)) <> 0
ORDER BY A.JOBNUMBR, A.PONUMBER