1

我有三张桌子。我需要将行值转换为列。

表格1:[Approval_Type]

App_ID  Type_Ds
2   RMC2
1   RMC1

表 2:[Project]

Pro_id  Summary
1   PROJECT1
2   PROJECT2

表3 [Prj_App]

App_Id  Pro_Id  ExpDt                           ComDt
1   2   2010-06-05                         2010-07-06 
1   1   1999-05-05                        1999-05-06
2   1   1900-01-01                        1900-01-05

我想将我的结果显示为

Pro_Id  RMC2    RMC2ExpeDt   RMC2ComDt  RMC1    RMC1ExpeDt  RMC1ComDt 
1         RMC2     1900-01-01    1900-01-05 RMC1    1999-05-05    1999-05-06
2         NULL      NULL             NULL   RMC1    2010-06-05    2010-07-06

下面是我返回的查询'

DECLARE @SQL1 NVARCHAR(MAX) = ''
DECLARE @SQL NVARCHAR(MAX) = ''

SELECT  @SQL1 = STUFF((SELECT ',' + QUOTENAME(Type_Ds) + ',' +  QUOTENAME(Type_Ds + ' Expected Date') + ',' + QUOTENAME(Type_Ds + ' Completed Date')
                    from dbo.AppType 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')         
        print @SQL1

SET @SQL = 'SELECT  *
            FROM    (   SELECT A.Pro_Id,
                                Type_DS
                        FROM  dbo.Project A  left join [dbo].[Prj_App] B on A.Pro_id = B.Pro_Id
                         right outer join dbo.AppType C on B.App_Id = C.App_ID
                    ) data
                    PIVOT
                    (   MAX(Type_DS)
                        FOR Type_DS IN (' + @SQL1 + ') 
                    ) pvt1                                        
                    where Pro_Id is not null'

print @SQL
EXECUTE SP_EXECUTESQL @SQL

Pro_Id  RMC2    RMC2ExpeDt   RMC2ComDt  RMC1    RMC1ExpeDt  RMC1ComDt 
1   RMC2    NULL     NULL             RMC1  NULL     NULL
2   NULL    NULL     NULL             RMC1  NULL     NULL.

任何人都可以帮助它...

4

1 回答 1

2

当您使用动态 SQL 时,我的建议是始终先编写硬编码查询,这样您就可以获得正确的逻辑,然后将其转换为动态 SQL。

由于您正在尝试旋转 3 列数据,我将首先取消旋转type_ds,expdt和 comdt` 列,然后应用 PIVOT 函数。

查询的硬编码版本将是:

SELECT  *
FROM    
(   
  select pro_id,
    type_ds = case 
                when col ='type_ds' 
                then type_ds 
                else type_ds+col end,
    value
  from
  (
     SELECT A.Pro_Id,
       c.Type_DS,
       convert(varchar(10), b.ExpDt, 120) ExpDt, 
       convert(varchar(10), b.ComDt, 120) ComDt
     FROM  dbo.Project A  
     left join [dbo].[Prj_App] B 
        on A.Pro_id = B.Pro_Id
     right outer join dbo.Approval_Type C 
        on B.App_Id = C.App_ID
   ) s
   cross apply
   (
      select 'type_ds', type_ds union all
      select 'expdt', expdt union all
      select 'comdt', comdt
   ) c (col, value)
) data
PIVOT
(   
  MAX(value)
  FOR Type_DS IN (RMC2, RMC2expdt, RMC2comdt,
                   RMC1, RMC1expdt, RMC1comdt) 
) pvt1                                        

请参阅SQL Fiddle with Demo。现在您有了查询的工作版本,您可以轻松地将其转换为动态 SQL:

DECLARE @SQL1 NVARCHAR(MAX) = ''
DECLARE @SQL NVARCHAR(MAX) = ''

SELECT  @SQL1 = STUFF((SELECT ',' + QUOTENAME(Type_Ds) + ',' +  QUOTENAME(Type_Ds + 'ExpDt') + ',' + QUOTENAME(Type_Ds + 'ComDt')
                    from dbo.Approval_Type 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')   


SET @SQL = 'SELECT  *
            FROM    
            (   
              select pro_id,
                type_ds = case 
                            when col =''type_ds'' 
                            then type_ds 
                            else type_ds+col end,
                value
              from
              (
                 SELECT A.Pro_Id,
                   c.Type_DS,
                   convert(varchar(10), b.ExpDt, 120) ExpDt, 
                   convert(varchar(10), b.ComDt, 120) ComDt
                 FROM  dbo.Project A  
                 left join [dbo].[Prj_App] B 
                    on A.Pro_id = B.Pro_Id
                 right outer join dbo.Approval_Type C 
                    on B.App_Id = C.App_ID
               ) s
               cross apply
               (
                  select ''type_ds'', type_ds union all
                  select ''expdt'', expdt union all
                  select ''comdt'', comdt
               ) c (col, value)
           ) data
           PIVOT
           (   
              MAX(value)
              FOR Type_DS IN (' + @SQL1 + ') 
           ) pvt1 '

--print @SQL
EXECUTE SP_EXECUTESQL @SQL

请参阅带有演示的 SQL Fiddle

于 2013-08-16T14:17:23.907 回答