获取数据透视结果的总和

Posted

技术标签:

【中文标题】获取数据透视结果的总和【英文标题】:get sums for pivot results 【发布时间】:2015-09-22 18:25:13 【问题描述】:
@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

【问题讨论】:

***.com/questions/18189188/t-sql-pivot-add-total-row 不知道他们在那个例子中做了什么。我对编码和学习仍然很陌生。 他们添加了 ROLLUP 命令,参考。 technet.microsoft.com/en-us/library/ms189305%28v=sql.90%29.aspx 我被要求按 ResellerCode、Pivo​​tResults.[20]、Pivo​​tResults.[30]、Pivo​​tResults.[40]、Pivo​​tResults.[50]、Pivo​​tResults.[60] 添加组汇总结果不正确..给我重复行几乎是金字塔外观结果方案 【参考方案1】:

这是我必须做的才能得到我想要完成的总和。我会发布一个 sn-p,但我没有足够的声誉。谢谢你的帮助

@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   SimSoldTo
        , PivotResults.[20]
        , PivotResults.[30]
        , PivotResults.[40]
        , PivotResults.[50]
        , PivotResults.[60]
        --Sum the total Numbers of PINS Sold
        , coalesce(PivotResults.[20],0) + coalesce(PivotResults.[30],0) +  coalesce(PivotResults.[40],0)
         +  coalesce(PivotResults.[50],0) +  coalesce(PivotResults.[60],0) as SimSoldCount
        --Sum the $ Amount ofthe PINS Sold
         , coalesce(PivotResults.[20],0)*20 + coalesce(PivotResults.[30],0)*30 +  coalesce(PivotResults.[40],0)*40
         +  coalesce(PivotResults.[50],0)*50 +  coalesce(PivotResults.[60],0)*60 as SumOfSales
        --SUM the % Commission being paid
         , coalesce(PivotResults.[20],0)*20 *.03 + coalesce(PivotResults.[30],0)*30 *.03 +  coalesce(PivotResults.[40],0)*40 *.03
         +  coalesce(PivotResults.[50],0)*50 *.03 +  coalesce(PivotResults.[60],0)*60 *.03 as "Commission Pay Out 3%"

        from(SELECT C.RESELLERSIMSOLDTO as SimSoldTo
                     ,iAdjustmentAmount as RefillAmount
                     ,count(iAdjustmentAmount) as AdjustmentValue

  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 iServiceClass like '531'
    and iRecordType like 'A'
    and iAdjustmentAmount >= '20'
    and C.ResellerActivationDate > DATEADD(year,-3,GETDATE())


  group by C.RESELLERSIMSOLDTO, iAdjustmentAmount

  )  as Great

     PIVOT
   (
 sum(AdjustmentValue) FOR RefillAmount IN ([20],[30],[40],[50],[60]) 
   ) AS PivotResults 

  order by SimSoldCount desc

【讨论】:

以上是关于获取数据透视结果的总和的主要内容,如果未能解决你的问题,请参考以下文章

使用 CASE 表达式透视数据

HIVE 数据透视和总和/计数

带有计数和总和的 SQL Server 数据透视表

Laravel Eloquent:获取嵌套关系中的数据总和并将其添加为新属性

python, pyspark : 获取 pyspark 数据框列值的总和

Excel 数据透视表中的最大值总和