@DateMonth as INT,
@DateYear as INT
AS
--set @DateMonth = 08
--SET @DateYear = 2015
DECLARE @FromDate DateTime;
DECLARE @ToDate DateTime;
SET @FromDate = CONVERT(DateTime, convert(varchar, @DateMonth) + '/01/' + convert(varchar, @DateYear))
SET @ToDate = EOMonth(@FromDate)
select * from (select C.RESELLERCODE, iAdjustmentAmount, count(iAdjustmentAmount) AS AdjTotal
FROM [EricssonRawData].[dbo].[tbl_EricssonRawData]
JOIN AirVoice.dbo.Customers C WITH(NOLOCK)
on C.SubscriberNumber = iSubscriberMSISDN
where convert(DateTime,substring(FileName,21,8)) between @FromDate and @ToDate
and iRecordType like 'A'
and iServiceClass = 531
and iAdjustmentType like 'ACTIVATION'
and iAdjustmentAmount >= '20'
group by RESELLERCODE, iAdjustmentAmount) as Test
PIVOT
(
sum(AdjTotal) FOR iAdjustmentAmount IN ([20],[30],[40],[50],[60])
) AS PivotResults
--group by ResellerCode
order by ResellerCode
当前结果..
RESELLERCODE 20 30 40 50 60
US3353 NULL 2 NULL NULL NULL
US3385 NULL 44 NULL 3 NULL
US3403 4 NULL NULL NULL NULL
US3341 NULL 2 NULL NULL NULL
我想要什么“总”
RESELLERCODE 20 30 40 50 60 TOTAL
US3341 NULL 2 NULL NULL NULL 2
US3353 NULL 2 NULL NULL NULL 2
US3385 NULL 44 NULL 3 NULL 47
US3403 4 NULL NULL NULL NULL 4