我需要在数据透视表中总计列

时间:2016-09-20 10:32:09

标签: sql-server tsql

DECLARE @FYEAR datetime='01-apr-2014';
DECLARE @TYEAR datetime='31-mar-2015';

Select * from (
select E.employeeID, E.employeename, S.netpay as n1
,((datename(MONTH,S.paydate)))as ShowDate 

FROM employee E
inner join salarydetail S
on E.employeeid=S.employeeid
Where S.paydate>= @FYEAR AND S.paydate<=@TYEAR and e.EmployeeID=1

) as t1

pivot (MAX(n1) for  ShowDate IN (APRIL,MAY,JUNE,JULY, AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER,JANUARY,FEBRUARY,MARCH, TOTAL ))  as P1 

我希望最后几个月的总数达到“总计”的水平。柱 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

DECLARE @FYEAR datetime='01-apr-2014';
DECLARE @TYEAR datetime='31-mar-2015';

Select *, (APRIL+MAY+JUNE+JULY+AUGUST+SEPTEMBER+OCTOBER+NOVEMBER+DECEMBER+JANUARY+FEBRUARY+MARCH) as Total from (
select E.employeeID, E.employeename, S.netpay as n1
,((datename(MONTH,S.paydate)))as ShowDate 

FROM employee E
inner join salarydetail S
on E.employeeid=S.employeeid
Where S.paydate>= @FYEAR AND S.paydate<=@TYEAR and e.EmployeeID=1

) as t1

pivot (MAX(n1) for  ShowDate IN (APRIL,MAY,JUNE,JULY, AUGUST,SEPTEMBER,OCTOBER,NOVEMBER,DECEMBER,JANUARY,FEBRUARY,MARCH ))  as P