如何在动态列上执行多个数据透视

时间:2019-05-13 07:28:21

标签: sql sql-server

我正在尝试对动态列进行多个数据透视,但是它给出了一个错误,正在执行分组,因为它返回了多个空列

选择列表中的列“ p.Parameter_1”无效。  样品表

CREATE TABLE [dbo].[TrainingDetails](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Tid] [int] NOT NULL,
    [TrainingName] [nvarchar](50) NULL,
    [TDate] [nvarchar](50) NULL,
    [TPlace] [nvarchar](50) NULL,
    [Trainer] [nvarchar](50) NULL,
    [Facilitator] [nvarchar](50) NULL,
    [TrainingSubject] [nvarchar](50) NULL
) 


CREATE TABLE [dbo].[TrainngParticpntDtls](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Tid] [int] NOT NULL,
    [PName] [nvarchar](50) NULL,
    [PRank] [nvarchar](50) NULL
) 

有人可以帮忙吗

所需的输出

enter image description here

输入 enter image description here

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

SELECT 
   @cols = STUFF(
(
    SELECT DISTINCT 
           ',' + QUOTENAME('Parameter_' + CAST(ROW_NUMBER() OVER(PARTITION BY [td].[TrainingName]
                           ORDER BY 
                                    [td].[tid]) AS VARCHAR(10)))
         , +',' + QUOTENAME('Desig_' + CAST(ROW_NUMBER() OVER(PARTITION BY [td].[Tplace]
                            ORDER BY 
                                     [td].[tid]) AS VARCHAR(10)))
    FROM 
         [TrainingDetails] AS [td]
    INNER JOIN [dbo].[TrainngParticpntDtls] AS [pd] ON [td].[Tid] = [pd].[Tid]
    WHERE [td].[TID] = 1 FOR
    XML PATH(''), TYPE
) .value
('.', 'NVARCHAR(MAX)'
), 1, 1, '');
        --print @cols

set @query = 'SELECT 
   [Tplace]
 , [TrainingName]
 , [TrainingSubject]
 , [TDate]
 , [Facilitator]
 , ' + @cols + '
FROM
    (
        SELECT 
               [td].[Tplace]
             , [td].[TrainingName]
             , [td].[Trainingsubject]
             , [td].[TDate]
             , [td].[Facilitator]
             , [pd].[pname]
             , [pd].[pDesig]
             , ''Parameter_'' + CAST(ROW_NUMBER() OVER(PARTITION BY [td].[TrainingName]
               ORDER BY 
                        [td].[Tid]) AS NVARCHAR) AS [r]
             , ''Desig_'' + CAST(ROW_NUMBER() OVER(PARTITION BY [td].[Tplace]
               ORDER BY 
                        [td].[tid]) AS NVARCHAR) AS [rn]
        FROM 
             [TrainingDetails] AS [td]
        INNER JOIN [dbo].[TrainngParticpntDtls] AS [pd] ON [td].[Tid] = [pd].[Tid]
        WHERE [td].[TID] = 1
    ) AS [x] PIVOT(MAX([Pname]) FOR [r] IN(' + @cols + ')) [p] 
             PIVOT(MAX([pDesig]) FOR [rn] IN(' + @cols + ')) [pv2]
GROUP BY 
         [Tplace]
       , [TrainingName]
       , [TrainingSubject]
       , [TDate]
       , [Facilitator]
ORDER BY 
         [TrainingName]'
execute(@query);

插入语句

INSERT INTO [dbo].[TrainingDetails] ([Tid],[TrainingName],[TDate],[TPlace],[Trainer],[Facilitator],[TrainingSubject])
     VALUES(1,'soft skills','31-03-2017','Webex','John','mary','Eng Grammer')

INSERT INTO [dbo].[TrainingDetails] ([Tid],[TrainingName],[TDate],[TPlace],[Trainer],[Facilitator],[TrainingSubject])

VALUES(2,“管理技能”,“ 31-03-2017”,“ Webex”,“史密斯”,“莎阿”,“管理ABC”)     走     插入[dbo] .TrainngParticpntDtlsVALUES(1,'pinto','SE')     插入[dbo] .TrainngParticpntDtlsVALUES(1,'Jack','PM')     插入[dbo] .TrainngParticpntDtlsVALUES(1,'harry','TL')     插入[dbo] .TrainngParticpntDtlsVALUES(1,'Rosy','HR')

INSERT INTO [dbo].[TrainngParticpntDtls]([Tid],[PName],[PRank])VALUES(2,'steve','Account')
INSERT INTO [dbo].[TrainngParticpntDtls]([Tid],[PName],[PRank])VALUES(2,'rony','DBA')
INSERT INTO [dbo].[TrainngParticpntDtls]([Tid],[PName],[PRank])VALUES(2,'logan','PM')
GO

0 个答案:

没有答案