Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/67.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/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
将行转换为列MS SQL_Sql_Sql Server 2008 - Fatal编程技术网

将行转换为列MS SQL

将行转换为列MS SQL,sql,sql-server-2008,Sql,Sql Server 2008,我有两个表,tblColumnNames和tblValues,具有以下结构和示例值: tblColumnNames - has two columns with names 'id' and 'ColumnName' and following example data: ------------------------------- id ColumnName 0 columnName1 1

我有两个表,
tblColumnNames
tblValues
,具有以下结构和示例值:

tblColumnNames - has two columns with names 'id' and 'ColumnName' 
and following example data:
-------------------------------
id                  ColumnName

0                   columnName1
1                   columnName2
2                   columnName3


tblValues has three columns 'id', 'ColumnId' and 'Value' with following example data:
-------------------------------

id                ColumnId                Value

51                0                       177
52                1                       abcde
53                2                       123
54                0                       40
55                1                       xyz
56                2                       321
ColumnId
对应于
tblColumnNames
表中的
id

如何连接这两个表,以便生成的列是
tblColumnNames
的行,并且相应的值取自
tblValues
的值列:

result
------------------------------
colummnName1        columnName2         columnName3

177                 abcde               123
40                  xyz                 321
编辑:
tblColumnNames
中的行数将不断变化。

您可以尝试此操作

DECLARE @ColNames NVARCHAR(MAX) =''
SELECT @ColNames = @ColNames + '['+ColumnName  +']'+ ', 'FROM tblColumnNames ORDER BY id
SET @ColNames = LEFT(@ColNames, LEN(@ColNames)-1)

DECLARE @TheQuery NVARCHAR(MAX)
SET @TheQuery = 'SELECT PVT.* FROM 
(SELECT V.Value, (V.id - V.ColumnId) RowGroupID, C.ColumnName FROM 
    tblValues V INNER JOIN tblColumnNames C ON V.ColumnId = C.id ) AS SRC 
        PIVOT ( MAX(VALUE) FOR ColumnName IN ('+ @ColNames +')) PVT'

EXEC sp_executesql @TheQuery
结果

RowGroupID  columnName1 columnName2 columnName3
----------- ----------- ----------- -----------
51          177         abcde       123
54          40          xyz         321

SQL查询没有动态列名。如果你想拥有动态名称,你需要使用动态SQL。可能是@AlbertoMartinez的重复。这个问题是寻找PIVOT的替代方案。我的问题是寻找一个总体解决方案。很抱歉,我忘了提到tblColumnNames中的行数(以及结果表的列数)将不断变化谢谢,所以基本上是关于
pivot
和动态生成列名