日期:2014-05-18  浏览次数:21295 次

不能对包含聚合或子查询的表达式执行聚合函数。
select sum(case when QtyPrgPlanPKID_FKID_QtyID in(select ListBudgetPKID from tbListBudget where ListBudgetName='801') then QtyPrgPlanPlanQty else 0 end) '801线路累计' from tbQtyPrgPlan


--不能对包含聚合或子查询的表达式执行聚合函数。

------解决方案--------------------
用left join去累计吧!

select sum(case when b.ListBudgetPKID is not null then a.QtyPrgPlanPlanQty 
else 0 end) '801线路累计' 
from tbQtyPrgPlan a left join tbListBudget b 
on a.QtyPrgPlanPKID_FKID_QtyID = b.ListBudgetPKID and b.ListBudgetName='801'
------解决方案--------------------
SQL code
--分别统计
select m.QtyPrgPlanPKID_FKID_QtyID , sum(QtyPrgPlanPlanQty) '801线路累计'
from tbQtyPrgPlan m , tbListBudget n
where m.QtyPrgPlanPKID_FKID_QtyID = n.ListBudgetPKID and n.ListBudgetName='801'
group by m.QtyPrgPlanPKID_FKID_QtyID

--总体统计
select sum(QtyPrgPlanPlanQty) '801线路累计'
from tbQtyPrgPlan m , tbListBudget n
where m.QtyPrgPlanPKID_FKID_QtyID = n.ListBudgetPKID and n.ListBudgetName='801'