将MS Access中的数据透视表转换为SQL Server中的数据透视表

时间:2019-04-04 03:22:36

标签: sql-server ms-access pivot pivot-table where-clause

我需要使用数据透视表将一些SQL语句从MS Access转换为SQL Server。我有一个视图[UETTDRSB39A Criteria]和一个表[RPLEvidence options]

我从未尝试过在SQL Server中使用数据透视表。

MS Access代码为:

TRANSFORM First([UETTDRSB39A Criteria].[RPL Link]) AS [FirstOfRPL Link]
SELECT [UETTDRSB39A Criteria].[RPL Link], [UETTDRSB39A Criteria].[RPLEvidence options].Detail
FROM [UETTDRSB39A Criteria], [2_Candidate Evidence records]
WHERE ((([UETTDRSB39A Criteria].Pathway) In ('Common','Electrical')))
GROUP BY [UETTDRSB39A Criteria].[RPL Link], [UETTDRSB39A Criteria].[RPLEvidence options].Detail
PIVOT [UETTDRSB39A Criteria].Prerequisite;

所需的(访问)输出为:Access Output Screenshot : Desired Output

错误的SQL Server查询为:

use LinesmanRPL
SELECT * from
(
Select distinct [RPL Link] as expr1, [RPL Link], [RPLEvidenceOptionsDetail], [Prerequisite] 
from [UETTDRSB39A Criteria], [2_Candidate Evidence records] 
where [UETTDRSB39A Criteria].Pathway = 'Common' or [UETTDRSB39A Criteria].Pathway = 'Electrical'
)temp
PIVOT  
(  
min(temp.expr1)  
FOR temp.[Prerequisite]
IN (Prereq1, Prereq2, Prereq3)  
) AS p
order by [RPL Link]

实际(SQL)输出为:SQL Output Screenshot : Actual Output

UETTDRSB39A Criteria

的示例数据

2_Candidate Evidence records

的示例数据

我还尝试了另一个SQL代码:

use LinesmanRPL
DECLARE @PivotColumnHeaders VARCHAR(max);
SELECT distinct @PivotColumnHeaders = COALESCE(@PivotColumnHeaders + ',['+ CAST(Prerequisite as varchar) + ']','['+ CAST(Prerequisite as varchar) + ']','['+ CAST(Prerequisite as varchar) + ']')
FROM [UETTDRSB39A Criteria]
where [UETTDRSB39A Criteria].Pathway='Common' or [UETTDRSB39A Criteria].Pathway='Electrical';
DECLARE @PivotTableSQL NVARCHAR(max);
SET @PivotTableSQL = N'
SELECT distinct *
FROM (
SELECT DISTINCT
    U.[RPL Link],
    U.Prerequisite,
    U.[RPLEvidenceOptionsDetail]
FROM [UETTDRSB39A Criteria] as U, [2_Candidate Evidence records] as C
    ) as pivotData
    pivot(
        min([RPL Link])
        for Prerequisite in (' + @PivotColumnHeaders +')
    ) as pivotTable    
'
;
execute(@PivotTableSQL);

此代码的输出为:SQL Output Screenshot : Actual Output 2

2 个答案:

答案 0 :(得分:1)

我认为您已使其变得不必要地复杂。我很确定您所需要的是以下内容:

SELECT * from
(
Select distinct RPL_Link as expr1, RPL_Link, RPLEvidence_Options_Detail, Prerequisite 
from UETTDRSB39A_Criteria
where UETTDRSB39A_Criteria.Pathway = 'Common' or UETTDRSB39A_Criteria.Pathway = 'Electrical'
)temp
PIVOT  
(  
min(temp.expr1)  
FOR temp.[Prerequisite]
IN ([UETTDREL16A], [UETTDRIS67A], [UETTDRSB39A])  
) AS p
ORDER BY RPL_LINK

请注意,在导入测试数据时,我使用下划线而不是空格,但是我认为应该清楚您需要做什么。

HTH

答案 1 :(得分:1)

查看@Jonathan Willcock提供的解决方案后,我能够为您的问题提供解决方案。看看这个,也许您可​​能需要完善它。

USE LinesmanRPL
DECLARE @PrerequisiteColumn AS NVARCHAR(MAX),
        @SQLforPivotTable  AS NVARCHAR(MAX);
SET @PrerequisiteColumn = STUFF((SELECT distinct ',' + QUOTENAME(Prerequisite) 
        FROM [UETTDRSB39A Criteria]
        WHERE [UETTDRSB39A Criteria].Pathway = 'Common' OR [UETTDRSB39A Criteria].Pathway = 'Electrical'
        FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'),1,1,'')
SET @SQLforPivotTable = 'SELECT RPLEvidenceOptionsDetail, ' + @PrerequisiteColumn + ' FROM 
        (
          SELECT [RPL Link], RPLEvidenceOptionsDetail, Prerequisite 
          FROM [UETTDRSB39A Criteria]
        ) Temp
        PIVOT 
        (
          MIN([RPL Link]) FOR Prerequisite IN (' + @PrerequisiteColumn + ')
        ) PivotTable '
EXECUTE(@SQLforPivotTable)