SQL Server中的动态表创建

时间:2014-12-11 14:00:15

标签: sql-server stored-procedures pivot

我有这样的情况

+---------+-----------+------------+
| FieldNo | FieldName | Substring  |
+---------+-----------+------------+
|       1 | A         | 8          |
|       1 | A         | A          |
|       1 | A         | DC         |
|       2 | B         | 7          |
|       3 | C         | 22         |
|       3 | C         | 37         |
+---------+-----------+------------+

需要这样的输出:

+----+------+------+
| A  |  B   |  C   |
+----+------+------+
| 8  | 7    | 22   |
| A  | Null | 37   |
| DC | Null | Null |
+----+------+------+

有关如何在SQL Server中执行此操作的任何建议吗?

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

select @cols = STUFF((SELECT ',' + QUOTENAME(Field_name) 
                    from bear_crossjoin
                    group by FIELD_NAME, FIELDNUMBER
                    order by FIELDNUMBER
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = N'SELECT ' + @cols + N' from 
             (
                select substring, Field_name
                from bear_crossjoin
            ) x
            pivot 
            (
                max(substring)
                for field_name in (' + @cols + N')
            ) p '

exec sp_executesql @query

2 个答案:

答案 0 :(得分:3)

我假设表名为tblTemp。试试这个

DECLARE @colList NVARCHAR(max)
DECLARE @query NVARCHAR(max)

select @colList = coalesce(@colList + ',', '') +  convert(varchar(12),TT.FIELDNAME)
from (SELECT DISTINCT FIELDNAME FROM tblTemp) TT
order by TT.FIELDNAME


SET @query = 'SELECT ' + @colList + ' FROM
(
SELECT row_number() over(partition by FIELDNO
                        order by FIELDNO) seq,
       FieldName, [Substring]
FROM tblTemp ) als
PIVOT
( MAX([SUBSTRING])
  FOR FieldName IN (' + @colList + ')
) piv'

exec sp_executesql @query

fiddle

答案 1 :(得分:2)

您似乎试图通过行号而不是直接在字段名称上进行PIVOT。下面的方法使用行号来获得所需的输出:

DECLARE @Data TABLE (FieldNo INT, FieldName VARCHAR(50), [Substring] VARCHAR(500))
INSERT @Data VALUES
    (1, 'A', '8'),
    (1, 'A', 'A'),
    (1, 'A', 'DC'),
    (2, 'B', '7'),
    (3, 'C', '22'),
    (3, 'C', '37')

;WITH DataRows AS (
    SELECT
        FieldName,
        [Substring],
        ROW_NUMBER() OVER (PARTITION BY FieldNo ORDER BY FieldName, [Substring]) AS RowNum
    FROM @Data
)
    SELECT
        CONVERT(VARCHAR(10), [A]) AS [A],
        CONVERT(VARCHAR(10), [B]) AS [B],
        CONVERT(VARCHAR(10), [C]) AS [C]
    FROM DataRows
        PIVOT (MAX([Substring]) FOR FieldName IN ([A], [B], [C])) T

这产生了所需的输出:

A          B          C
---------- ---------- ----------
8          7          22
A          NULL       37
DC         NULL       NULL