SQL如何将几条语句查询结果求和
selectISNULL(sum(casewhentotalweightlimit=17000andoverloadweight>170thenvehcountelse0...
select ISNULL(sum(case when totalweightlimit=17000 and overloadweight>170 then vehcount else 0 end),0)一型,ISNULL(sum(case when totalweightlimit=25000 and overloadweight>250 then vehcount else 0 end),0)二型,ISNULL(sum(case when totalweightlimit=35000 and overloadweight>350 then vehcount else 0 end),0)三型,ISNULL(sum(case when totalweightlimit=43000 and overloadweight>430 then vehcount else 0 end),0)四型,ISNULL(sum(case when totalweightlimit=49000 and overloadweight>490 then vehcount else 0 end),0)五型from LaneExList where squaddate between '20171101'and'20171130'and exvehicleflag=2各位高手,以上语句是我平常查询使用的语句。我现在是想在FROM前加一条求和语句,目的把以上的“一型”至“五型”这五条语句的查询结果进行求和。感谢各位!
展开
1个回答
展开全部
select 一型,。。。,五型, 一型+...+五型 as 和
from(
select ISNULL(sum(case when totalweightlimit=17000 and overloadweight>170 then vehcount else 0 end),0)一型,ISNULL(sum(case when totalweightlimit=25000 and overloadweight>250 then vehcount else 0 end),0)二型,ISNULL(sum(case when totalweightlimit=35000 and overloadweight>350 then vehcount else 0 end),0)三型,ISNULL(sum(case when totalweightlimit=43000 and overloadweight>430 then vehcount else 0 end),0)四型,ISNULL(sum(case when totalweightlimit=49000 and overloadweight>490 then vehcount else 0 end),0)五型from LaneExList where squaddate between '20171101'and'20171130'and exvehicleflag=2
) abc
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询