Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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 Server动态数据透视表_Sql_Sql Server_Pivot - Fatal编程技术网

SQL Server动态数据透视表

SQL Server动态数据透视表,sql,sql-server,pivot,Sql,Sql Server,Pivot,在SQL Server中,我有两个表TableA和TableB,基于它们我需要生成一个非常复杂的报告,在做了一些研究之后,我得出结论,我必须使用SQL Pivot表。另外,我尝试了这个示例,但是在我的例子中,TableB表可以有任意数量的子行,这使得它非常复杂,所以,有人能在这方面帮助我吗。详情如下: 代码 样本数据 ProjectID | ControlID | ControlCode | ControlPoint | ControlScore | ControlValue P001

在SQL Server中,我有两个表TableA和TableB,基于它们我需要生成一个非常复杂的报告,在做了一些研究之后,我得出结论,我必须使用SQL Pivot表。另外,我尝试了这个示例,但是在我的例子中,TableB表可以有任意数量的子行,这使得它非常复杂,所以,有人能在这方面帮助我吗。详情如下:

代码

样本数据

ProjectID | ControlID | ControlCode | ControlPoint | ControlScore | ControlValue
P001        1           A            30.44            65           Invalid
P001        2           C            45.30            85           Valid
ControlID | ControlChildID | ControlChildValue
1           100              Yes
1           101              No
1           102              NA  
1           103              Others 
2           104              Yes
2           105              SomeValue
代码

样本数据

ProjectID | ControlID | ControlCode | ControlPoint | ControlScore | ControlValue
P001        1           A            30.44            65           Invalid
P001        2           C            45.30            85           Valid
ControlID | ControlChildID | ControlChildValue
1           100              Yes
1           101              No
1           102              NA  
1           103              Others 
2           104              Yes
2           105              SomeValue
对于给定的projectd,输出应该在一行中,首先是它的所有控制值,然后是基于控制代码的子控制值,即控制代码\子1、2、3。。。应该是这样的


我对SQL Pivot功能一无所知-您尝试过什么?这里有很多关于使用pivot的问题-请参阅右侧的相关问题。阅读它们和链接,尝试一些东西,然后问你什么时候卡住了。有一套标准的子值吗?或者最大数量的子值?这是什么关系数据库管理系统?@Hogan:没有最大数量的子值,我使用的是SQL Server。另外,尝试了一些示例,但我的情况很微妙。@user972255-如果您没有最大数量的子对象,则无法使用pivot执行此操作,必须使用动态SQL。同样正确,因为在转换之前您不知道列名。