透视未知数量的列值
Pivot Unknown Number Of Column Values
我正在尝试创建一个自动数据字典。我加入了所有 tables' table 字段' table 和查找 table 以获取查找值(字段的下拉值)作为...
+-----------+----------------+---------------+
| TableName | Field Name | Lookup values |
+-----------+----------------+---------------+
| Pathology | Medical Report | Avaliable |
| Pathology | Medical Report | Not Avaliable |
| Pathology | Medical Report | Pending |
+-----------+----------------+---------------+
大约有 200+ tables (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
我不太清楚如何获得所需的结果集。
有人可以帮忙吗?
我猜你需要这样的东西:
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;
当然,使用您的示例数据,结果将是:
我相信您可以使用代码来解决您的问题。没有什么复杂的 - 您只需要构建一个字符串,其中包含执行 PIVOT
的值。
我正在尝试创建一个自动数据字典。我加入了所有 tables' table 字段' table 和查找 table 以获取查找值(字段的下拉值)作为...
+-----------+----------------+---------------+
| TableName | Field Name | Lookup values |
+-----------+----------------+---------------+
| Pathology | Medical Report | Avaliable |
| Pathology | Medical Report | Not Avaliable |
| Pathology | Medical Report | Pending |
+-----------+----------------+---------------+
大约有 200+ tables (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
我不太清楚如何获得所需的结果集。
有人可以帮忙吗?
我猜你需要这样的东西:
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;
当然,使用您的示例数据,结果将是:
我相信您可以使用代码来解决您的问题。没有什么复杂的 - 您只需要构建一个字符串,其中包含执行 PIVOT
的值。