如何使用动态列-SQL显示数据

时间:2018-01-05 14:55:21

标签: sql dynamic-columns

我有一个动态数据透视查询,其中包含动态列column1,第2列......列

我的查询工作正常。我可以将它转储到临时表中......但我想要一个视图。由于列数不断变化,我无法做到。我该怎么办?将这个查询放在一起需要很长时间,因为我不是sql专家。 我想查看结果,以便将其链接到团队的Access。我还必须根据此链接在Access中创建更多查询,以便链接无法断开连接。任何帮助将不胜感激 以下是我的询问。

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

  SELECT @cols = STUFF((SELECT ',' 
                      + QUOTENAME(case 
                                    when d.col = 'OriginalDx' then col+cast(seq as varchar(10))
                                    else 'OriginalDx'+cast(seq as varchar(10))+'_'+col end) 
                from 
                (
                  select row_number() over(partition by [HCCCodingBASEID]
                                            order by [HCCCodingBASEID]) seq
                  from [IDEAApplication].[HCCCoding].[OriginalDiagnosis]
                ) t
                cross apply
                (
                  select 'OriginalDx', 1 

                ) d (col, so)
                group by col, so, seq
                order by seq, so
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

   set @query = 'SELECT [HCCCodingBASEID] AS ORIGINALDX_ID, ' + @cols + ' 
        from 
        (
          select t.[HCCCodingBASEID], 
            col = case 
             when c.col = ''OriginalDx'' then col+cast(seq as varchar(10))
              else ''OriginalDx''+cast(seq as varchar(10))+''_''+col
            end,
            value
          from 
          (
            select [HCCCodingBASEID], [OriginalDiagnosisCD],
              row_number() over(partition by [HCCCodingBASEID]
                                order by [HCCCodingBASEID]) seq
            from [IDEAApplication].[HCCCoding].[OriginalDiagnosis]
          ) t
          cross apply
         (
            select ''OriginalDx'', [OriginalDiagnosisCD] 

          ) c (col, value)
        ) x
        pivot 
        (
            max(value)
            for col in (' + @cols + ')
        ) p '

   ----execute sp_executesql @query;

  DECLARE @cols1 AS NVARCHAR(MAX),
          @query1  AS NVARCHAR(MAX)

  select @cols1 = STUFF((SELECT ',' 
                      + QUOTENAME(case 
                                   when d.col = 'DxAdded' then col+cast(seq as varchar(10))
                                    else 'DxAdded'+cast(seq as varchar(10))+'_'+col end) 
                from 
                (
                  select row_number() over(partition by [HCCCodingBASEID]
                                            order by [AddDiagnosisCD]) seq
                  from [IDEAApplication].[HCCCoding].[AddDiagnosis]
                ) t1
                cross apply
                (
                  select 'DxAdded', 1 union all
                  select 'Reason', 2
                ) d (col, so)
                group by col, so, seq
                order by seq, so
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

   set @query1 = 'SELECT [HCCCodingBASEID] AS DXADDED_ID, ' + @cols1 + ' 
        from 
        (
          select t1.[HCCCodingBASEID], 
            col = case 
             when c.col = ''DxAdded'' then col+cast(seq as varchar(10))
              else ''DxAdded''+cast(seq as varchar(10))+''_''+col
            end,
            value
          from 
          (
            select [HCCCodingBASEID], [AddDiagnosisCD], [AddReasonTXT],
              row_number() over(partition by [HCCCodingBASEID]
                                order by [AddDiagnosisCD]) seq
            from [IDEAApplication].[HCCCoding].[AddDiagnosis]
          ) t1
          cross apply
         (
            select ''DxAdded'', [AddDiagnosisCD] union all
            select ''Reason'', [AddReasonTXT]
          ) c (col, value)
        ) x
        pivot 
        (
            max(value)
            for col in (' + @cols1 + ')
        ) p '

  ------------------------------------------------------------------
  DECLARE @cols2 AS NVARCHAR(MAX),
          @query2  AS NVARCHAR(MAX)

  select @cols2 = STUFF((SELECT ',' 
                      + QUOTENAME(case 
                                   when d.col = 'DxDeleted' then col+cast(seq as varchar(10))
                                    else 'DxDeleted'+cast(seq as varchar(10))+'_'+col end) 
                from 
                (
                  select row_number() over(partition by [HCCCodingBASEID]
                                            order by [DeleteDiagnosisCD]) seq
                  from [IDEAApplication].[HCCCoding].[DeleteDiagnosis]
                ) t2
                cross apply
                (
                  select 'DxDeleted', 1 union all
                  select 'Reason', 2
                ) d (col, so)
                group by col, so, seq
                order by seq, so
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    ,1,1,'')

   set @query2 = 'SELECT [HCCCodingBASEID] AS DXDELETED_ID, ' + @cols2 + ' 
       -----into ##tmp2
        from 
        (
          select t2.[HCCCodingBASEID], 
            col = case 
             when c.col = ''DxDeleted'' then col+cast(seq as varchar(10))
              else ''DxDeleted''+cast(seq as varchar(10))+''_''+col
            end,
            value
          from 
          (
            select [HCCCodingBASEID], [DeleteDiagnosisCD], [DeleteReasonTXT],
              row_number() over(partition by [HCCCodingBASEID]
                                order by [DeleteDiagnosisCD]) seq
            from [IDEAApplication].[HCCCoding].[DeleteDiagnosis]
          ) t2
          cross apply
         (
            select ''DxDeleted'', [DeleteDiagnosisCD] union all
            select ''Reason'', [DeleteReasonTXT]
          ) c (col, value)
        ) x
        pivot 
        (
            max(value)
            for col in (' + @cols2 + ')
        ) p '
 -----------------------------------------------------merge all tables
  ----IF OBJECT_ID('tempdb..##TMP') IS NOT NULL
     ---DROP TABLE ##TMP

   DECLARE @cmd NVARCHAR(MAX);
   SET @cmd=N'
   SELECT top 100 percent
           A.*,
       BB.PatientID as [Epic PatientID],
       convert (varchar (10),AA.BirthDTS,101) as [Birth Date],
           AA.SexDSC as [Sex],
           BB.HospitalAccountBaseClassDSC as [Patient Type],
       EE.payorNM AS [Payor Name],
    B.*,
    C.*,
    D.*
            --INTO ##TMP
    FROM
    [IDEAApplication].[HCCCoding].[HCCCoding] as A
           LEFT JOIN ('+@query+') AS B 
     ON A.ID=B.ORIGINALDX_ID
           LEFT JOIN ('+@query1+') AS C
             ON A.ID=C.DXADDED_ID
       LEFT JOIN ('+@query2+') AS D
     ON A.ID=D.DXDELETED_ID
       LEFT OUTER JOIN Epic.Finance.HospitalAccount_Enterprise AS BB
             ON A.HospitalAccountID = BB.HospitalAccountID
       LEFT JOIN Epic.Patient.Patient_Enterprise AS AA
             ON  BB.PatientID =AA.PatientID 
           LEFT JOIN Epic.Finance.HospitalAccount3_Enterprise AS CC
             ON BB.HospitalAccountID = CC.HospitalAccountID
           LEFT JOIN Epic.Reference.Payor AS EE
             ON BB.PrimaryPayorID = EE.PayorID
     order BY A.ID

     ;
  ';
  EXECUTE (@cmd)

0 个答案:

没有答案