MS-SQL 增加2列分组问题.
存储过程如下:
Alter proc query_consume_Sum @dateStart datetime,@dateEnd datetime,@code varchar(10)
as
select b.name,b.code,d.name,
sex=case b.sex when 0 then N'男' else N'女' end,
e.longname,b.pydate, Sumzaocan= sum(a.zaocan),Sumwucan=sum(a.wucan),Sumwancan=sum(a.wancan),Sumyexiao=sum(a.yexiao),Sumchencan=sum(a.chencan),
sum(case when zaocan=1 then 1 ELSE 0 END) AS zaocanBZ,
sum(case when wucan>=4 and (b.zhiwu<='041' or b.zhiwu='052') then 1 when wucan>=4 and (b.zhiwu>'041' and b.zhiwu<>'052') then 5 else 0 end) AS wucanBZ, --041 主管,052工程师.主管及以下人员用餐则补助1元.其余补助5元.不用餐不补助
sum(case when wancan>=4 and (b.zhiwu<='041' or b.zhiwu='052') then 1 when wancan>=4 and (b.zhiwu>'041' and b.zhiwu<>'052') then 5 else 0 end) AS wancanBZ,
sum(case when yexiao>=4 and ((b.zhiwu>'041' and b.zhiwu<>'052') or c.ClockTimes>0) then 5 when yexiao>=4 then 1 else 0 end) AS yexiaoBZ,
sum(case when chencan>=2 then 2 else 0 end) AS chencanBZ
from Uv_Consume_EachDay a inner join zlemployee b on a.empid=b.id
left join uv_JudgeIsSubsidy c on a.empid=c.empid and a.date=c.date
left join E_zhiwu d on b.zhiwu=d.code
left join ZLdept e on b.dept=E.CODE
where (b.code=@code or isnull(@code,'')='') and a.date between @dateStart and @dateEnd
group by b.name,b.code,e.longname,b.pydate, d.name,sex
order by b.code
执行以下语句时exec query_consume_Sum '2014-03-01','2014-04-01',''
如何增加两列SumConsume,SumBZ.分别为Sumzaocan ,Sumwucan ,Sumwancan ,Sumyexiao ,Sumchencan 的和
与 zaocanBZ, wucanBZ, wancanBZ ,yexiaoBZ ,chencanBZ 的和.如何修改存储过程?
图片附加不过去,没办法,只能发纯文字了
------解决方案--------------------CREATE PROC query_consume_Sum
@dateStart DATETIME ,
@dateEnd DATETIME ,
@code VARCHAR(10)
AS
SELECT name,code,name,sex,Sumzaocan+Sumwucan+Sumwancan+Sumyexiao+Sumchencan,zaocanBZ+wucanBZ+wancanBZ+yexiaoBZ+chencanBZ
FROM
(
SELECT
b.name, b.code, d.name, sex = CASE b.sex
WHEN 0 THEN N'男'
ELSE N'女'
END, e.longname,