-1

在不执行任何聚合的情况下旋转多级标头

不确定如何真正获得此多级标题而在数据透视图中没有聚合,因为您会注意到当前和所需的布局。

With pivot_data as
(
Select * from Table1
)
Select id, 3/31/2001, 6/30/2001, 9/30/2001, 12/31/2001, 3/31/2002, 6/30/2002, 9/30/2002, 12/31/2002
from pivot_data

Pivot(Max(Comm) For Dt in (3/31/2001, 6/30/2001, 9/30/2001, 12/31/2001, 3/31/2002, 6/30/2002, 9/30/2002, 12/31/2002)) P1
Pivot(Max(Norm)) For Dt in (3/31/2001, 6/30/2001, 9/30/2001, 12/31/2001, 3/31/2002, 6/30/2002, 9/30/2002, 12/31/2002)) P2
Pivot(Max(Team)) For Dt in (3/31/2001, 6/30/2001, 9/30/2001, 12/31/2001, 3/31/2002, 6/30/2002, 9/30/2002, 12/31/2002)) P3

我正在尝试实现这种布局:

    Comm    Comm    Comm    Comm    Norm    Norm    Norm    Norm
id  3/31/2018   6/30/2018   9/30/2018   12/31/2018  3/31/2018   6/30/2018   9/30/2018   12/31/2018
1   55  0   54  0   0   3   0   3
2   0   41  0   43  3   0   4   0


From Current layout:

id  Date        Comm    Norm
1   3/31/2018   55  
1   6/30/2018       3
1   9/30/2018   54  
1   12/31/2018      3
2   3/31/2018       3
2   6/30/2018   41  
2   9/30/2018       4
2   12/31/2018  43
4

1 回答 1

1

您可以通过这种方式使用条件聚合:

select id,
    sum(case when Date = '3/31/2018' then Comm end) as 'Comm 3/31/2018',
    sum(case when Date = '6/30/2018' then Comm end) as 'Comm 6/30/2018',
    sum(case when Date = '9/30/2018' then Comm end) as 'Comm 9/30/2018',
    sum(case when Date = '12/31/2018' then Comm end) as 'Comm 12/31/2018',
    sum(case when Date = '3/31/2018' then Norm end) as 'Norm 3/31/2018',
    sum(case when Date = '6/30/2018' then Norm end) as 'Norm 6/30/2018',
    sum(case when Date = '9/30/2018' then Norm end) as 'Norm 9/30/2018',
    sum(case when Date = '12/31/2018' then Norm end) as 'Norm 12/31/2018'
from mytable
group by id
于 2019-09-06T00:53:20.597 回答