透视未知数量的列值

时间:2017-05-23 23:42:17

标签: sql sql-server tsql pivot

我正在尝试创建一个自动数据字典。我加入了所有桌子'表格字段' table和lookup table将Lookup值(下拉字段值)作为...

+-----------+----------------+---------------+
| TableName | Field Name     | Lookup values |
+-----------+----------------+---------------+
| Pathology | Medical Report | Avaliable     |
| Pathology | Medical Report | Not Avaliable |
| Pathology | Medical Report | Pending       |
+-----------+----------------+---------------+

大约有200多个表(TableName)和5000多列(Field Name)。

必填结果

+-----------+----------------+
| TableName | Medical Report |
+-----------+----------------+
| Pathology | Avaliable      |
| Pathology | Not Avaliable  |
| Pathology | Pending        |
+-----------+----------------+

到目前为止,我已尝试使用SQL中的PIVOT函数,但没有成功,因为无法应用聚合函数,因为不存在标识列。

我的代码摘录

SELECT TableName,
       Field1,
       Field2,
       ...,
       Field2000+ (this not possible as there are so many columns)
FROM ( result set
     )
PIVOT
(
    aggregated function doesn't apply as no identity column is present
    FOR ( FieldName ) IN ( Field1,
                           Field2,
                           ...,
                           Field2000+ (this not possible as there are so many columns)
                         )
) AS pivotTable

我不确切知道如何获得所需的结果集。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

我猜你需要这样的东西:

IF OBJECT_ID('tempdb..#DataSource') IS NOT NULL
BEGIN;
    DROP TABLE #DataSource;
END;

CREATE TABLE #DataSource
(
    [TableName] SYSNAME
   ,[FieldName] NVARCHAR(64)
   ,[LookupValues] NVARCHAR(64)
);

INSERT INTO #DataSource ([TableName], [FieldName], [LookupValues])
VALUES ('Pathology', 'Medical Report', 'Avaliable')
      ,('Pathology', 'Medical Report', 'Not Avaliable')
      ,('Pathology', 'Medical Report', 'Pending')
      ,('Pathology', 'Laboratory Report', 'Avaliable')
      ,('Pathology', 'Laboratory Report', 'Not Avaliable')
      ,('Pathology', 'Laboratory Report', 'Pending')
      ,('Pathology', 'Laboratory Report', 'Declined')
      ,('Pathology', 'Laboratory Report', 'Private')
      ,('Pathology', 'Laboratory Report', 'Rejected')
      ,('Oncology', 'Laboratory Report', 'Avaliable')
      ,('Oncology', 'Laboratory Report', 'Not Avaliable')
      ,('Oncology', 'Laboratory Report', 'Pending')
      ,('Morgue', 'Death Report', 'Type 1')
      ,('Morgue', 'Death Report', 'Type 2')
      ,('Morgue', 'Death Report', 'Type 3')
      ,('Morgue', 'Death Report', 'Type 4');

DECLARE @DynamicSQLStatement NVARCHAR(MAX)
       ,@PIVOTcolumns NVARCHAR(MAX);

SELECT @PIVOTcolumns = STUFF
(
    (
        SELECT DISTINCT ',[' + [FieldName] + ']'
        FROM #DataSource
        FOR XML PATH(''), TYPE
    ).value('.', 'nvarchar(max)')
    ,1
    ,1
    ,''
);

SET @DynamicSQLStatement = N'
SELECT *
FROM
(
    SELECT *
          ,ROW_NUMBER() OVER (PARTITION BY [TableName], [FieldName] ORDER BY [LookupValues]) AS [RowID]
    FROM #DataSource
) DS
PIVOT
(
    MAX([LookupValues]) FOR [FieldName] IN (' + @PIVOTcolumns + ')
) PVT
ORDER BY [TableName];'

EXEC sp_executesql @DynamicSQLStatement;

enter image description here

当然,使用您的样本数据,结果将是:

enter image description here

我相信您可以使用该代码来解决您的问题。没有什么复杂的 - 您只需要构建一个包含执行PIVOT的值的字符串。