Sql server 动态数据透视表sql server 2008

Sql server 动态数据透视表sql server 2008,sql-server,dynamic,pivot,Sql Server,Dynamic,Pivot,我想做数据挖掘。但我的数据毫无用处。 我的表格结构类似于: date customerid age residence prosubsclassid productid ---------------------------------------------------------------------------- 21.11.2001 123232323 a b 2099

我想做数据挖掘。但我的数据毫无用处。 我的表格结构类似于:

date            customerid     age   residence   prosubsclassid   productid       
----------------------------------------------------------------------------
21.11.2001       123232323      a         b          2099         23232322 


amount   asset    sales
------------------------
4        34       56
现在我必须以这种方式显示数据:

prosubsclassid     130207   130208  130209      
                   ------   ------  ------  
1413232              1        3       1     
3435545              2        1       2      
3534344                       3       1      sum(amount)
Column(customerid)    

我想在我的数据中转换成表格格式。

没有自动的方法可以做到这一点。SQL server中支持pivot,但仍需要指定列

根据您想要的是文本报告还是具有动态列的表,我将按日期和ProSubClassID对数据进行分组,然后使用光标构建数据

  • 如果需要动态表,请基于分组数据构建动态sql查询并运行Exec
  • 如果您想要文本报告,只需按照您希望的每行方式将字符串数据连接到 包含一个文本列的临时表,完成后,选择该表