在SQL中显示年度摘要数据

时间:2017-02-16 17:31:22

标签: sql-server

我正在尝试以原始格式显示我的年度摘要数据。 当前格式显示客户#,年份和每月的总金额。示例。列名称为customer,年份为0,月份为1-12

customer     0         1          2           3           4          5          6             7           8           9          10           11         12
VALLERO     2014    634.150000  560.740000  254.670000  370.292500  99.225000   157.426666  358.650000  190.925000  767.515000  71.665000   587.305000  615.525000
VALLERO     2015    634.150000  560.740000  254.670000  370.292500  99.225000   157.426666  358.650000  190.925000  767.515000  71.665000   587.305000  615.525000

我现在需要的是将格式更改为类似的内容。

 customer  year1  total1   year2  total2     
 VALLERO   2014    0.00    2015    0.00

2 个答案:

答案 0 :(得分:1)

如果"年#"基于客户的生命周期价值或相对于客户体验的价值。如果不相对,请删除Partition By Customer

轻松扩展列数甚至动态。

您将在2017年注意到新客户,并且他的第1年是2017年

Declare @YourTable table (customer varchar(25),[0] int,[1] money,[2] money,[3] money,[4] money,[5] money,[6] money,[7] money,[8] money,[9] money,[10] money,[11] money,[12] money)
Insert Into @YourTable values
('VALLERO' ,2014 ,634.150000 ,560.740000 ,254.670000 ,370.292500 ,99.225000 ,157.426666 ,358.650000 ,190.925000 ,767.515000 ,71.665000 ,587.305000 ,615.525000),
('VALLERO' ,2015 ,634.150000 ,560.740000 ,254.670000 ,370.292500 ,99.225000 ,157.426666 ,358.650000 ,190.925000 ,767.515000 ,71.665000 ,587.305000 ,615.525000),
('NewCust' ,2017 ,500.000000 ,650.000000 ,null       ,null       ,null      ,null       ,null       ,null       ,null       ,null       ,null      ,null      )


Select Customer
      ,Year1    = max(case when YearNr= 1 then [0] end)
      ,Total1   = sum(case when YearNr= 1 then Value end)
      ,Year2    = max(case when YearNr= 2 then [0] end)
      ,Total2   = sum(case when YearNr= 2 then Value end)
From (
        Select Customer
              ,YearNr=Dense_Rank() over (Partition By Customer Order by [0])
              ,[0]
              ,Value
         From  @YourTable A
         UnPivot (Value for Item in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12]) ) u
     ) A
 Group By Customer

<强>返回

enter image description here

答案 1 :(得分:0)

假设几个列不为空。

试试这个:

select
    customer,
    max(case when year = 2014 then year end) year1,
    max(case when year = 2014 then total end) total1,
    max(case when year = 2015 then year end) year2,
    max(case when year = 2015 then total end) total2
from (
    select
        customer,
        [0] year,
        [1] + [2] + [3] + [4] + [5] + [6] + [7] + [8] + [9] + [10] + [11] + [12] total
    from t
) t
group by customer;

如果他们只是将列包装到coalesce

select
    customer,
    max(case when year = 2014 then year end) year1,
    sum(case when year = 2014 then total end) total1,
    max(case when year = 2015 then year end) year2,
    sum(case when year = 2015 then total end) total2
from (
    select
        customer,
        [0] year,
        coalesce([1],0) + coalesce([2],0) + coalesce([3],0) + coalesce([4],0) 
        + coalesce([5],0) + coalesce([6],0) + coalesce([7],0) + coalesce([8],0)
        + coalesce([9],0) + coalesce([10],0) + coalesce([11],0) + coalesce([12],0) total
    from t
) t
group by customer;