Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL中透视以实现多级标头_Sql_Aqua Data Studio - Fatal编程技术网

如何在SQL中透视以实现多级标头

如何在SQL中透视以实现多级标头,sql,aqua-data-studio,Sql,Aqua Data Studio,不执行任何聚合的透视多级标头 不确定如何在pivot中没有聚合的情况下真正获得这个多级标题,因为您会注意到当前和所需的布局 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 (

不执行任何聚合的透视多级标头

不确定如何在pivot中没有聚合的情况下真正获得这个多级标题,因为您会注意到当前和所需的布局

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

可以通过以下方式使用条件聚合:

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

“不执行任何聚合”-这意味着什么?Mysql不支持pivot语句,因此上面的代码不可能与Mysql相关。@CaiusJard只需要pivot而不聚合Comm,Norm列。我已经分享了当前和想要的布局。这说明了吗?Pivot(Max(Comm)…(不确定使用Max是否正确)因此提到不需要聚合*@Shadow在SQL中我们可以使用Pivot,如果您知道从当前布局实现所需布局的方法(问题中共享了两种布局).这让你困惑吗?我可以进一步澄清是否需要。neeraj发布了一些可以产生你想要的输出的东西,但它确实需要使用聚合。这是你想要的吗?我想,这正是他说的“不执行任何聚合”的地方这让我很困惑。同样的事情也可以在多个连接中执行suppose@CaiusJard-混乱的语言“不确定如何在枢轴中没有聚合的情况下真正获得此多级标题”。我提供了这个解决方案,因为它通过条件聚合来实现pivot所能实现的功能。@CaiusJard当我说“不聚合”时,我指的是Comm列和Norm列。很抱歉,这让您感到困惑。您知道如何在SQL中使用pivot函数来实现这一点吗?@NeerajAgarwal我知道您的代码是什么,但它作为一个列标题Comm 3/31/2018提供。我nt将此单独保存,正如您在我上面共享的所需输出中所注意到的,使用PIVOT有任何解决方案吗?是的,当我说“不聚合”时,我指的是Comm和Norm列。抱歉,如果这让您感到困惑。Neeraj的代码生成的正是您所要求的输出。您是说您希望在标题中使用换行符,以便Comm是与2018年3月31日不同?