日期:2014-05-17  浏览次数:21020 次

“Sum”函数的使用
存储过程如下:

ALTER proc [dbo].[Sp_GetWaterData]
@DAYear char(4),
@DAMonth char(2)
as
if @DAMonth is null
select r.RegionName , sum(t.SumQty) as SumQty, YearMonth from tjshuru as t inner join Region as r on t.RegionNo=r.RegionNo where t.YearMonth like (@DAYear+'%') and t.EnergyType='WM'
else  
select r.RegionName , t.SumQty as SumQty, YearMonth from tjshuru as t inner join Region as r on t.RegionNo=r.RegionNo where t.YearMonth=(@DAYear+@DAMonth) and t.EnergyType='WM'

问题:“sum(t.SumQty)”提示错误,信息如下:
消息 8120,级别 16,状态 1,过程 Sp_GetWaterData,第 6 行
选择列表中的列 'Region.RegionName' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。

怎样根据“where t.YearMonth like (@DAYear+'%')”来对“t.SumQty”求和?