SQL Dynamic Pivot没有聚合

时间:2014-11-03 21:47:57

标签: sql sql-server dynamic pivot

我发现了一些与我需要的东西类似的东西,但是我无法将自己的大脑包裹在如何将它带到需要去的地方。

这肯定有帮助:simple(?) PIVOT without an aggregate


这是我当前的查询:

SELECT  [1] AS Lien1
   ,[2] AS Lien2
   ,[3] AS Lien3
   ,[4] AS Lien4
   ,[5] AS Lien5
   ,[6] AS Lien6
FROM    ( SELECT    lt.Name AS [LienName]
            --   ,LienID 
               ,ROW_NUMBER() OVER ( ORDER BY LienID ) AS RN
      FROM      dbo.Lien AS L
                INNER JOIN dbo.LienType AS LT ON LT.LienTypeID = L.LienTypeID
      WHERE     FileID = 528267
    ) AS PivotSource PIVOT

( MAX(LienName) FOR RN IN ( [1], [2], [3], [4], [5], [6] ) ) piv

返回以下结果:

Lien1   Lien2   Lien3   Lien4   Lien5   Lien6
Deed of Trust   Assignment  Appointment of Substitute Trustee   Assignment  Assignment  Civil Foreclosure Case

我需要注释行( - LienID)不被注释掉并在相应的LienName之后返回LienID(为了格式化目的,我删除了最后四列)。

Lien1            LienID Lien2        LienID    
Deed of Trust    123    Assignment   234       

也许Pivot不是最好的方法,但它是迄今为止我发现的最好的方式。我有5个表返回多行,我需要返回一行上的所有值。我一直试图在这里理解动态SQL Pivot问题,但我无法执行一个来执行我需要它执行的方式。

我愿意接受任何有用的建议 - 提前感谢您的时间!


编辑:11/4/14 @ 10:47 AM

我想回来发布正在进行的工作,因为我把这个答案变成了一些动态查询。

DECLARE @FileID INT = 528267 
DECLARE @Cols NVARCHAR(MAX)
DECLARE @query NVARCHAR(MAX)

SELECT  @cols = STUFF((SELECT   ',' + QUOTENAME('LienNumber' + CONVERT(VARCHAR,     ROW_NUMBER() OVER ( ORDER BY SortOrder )))
                   FROM     dbo.Lien AS L
                            INNER JOIN dbo.LienType AS LT ON LT.LienTypeID = L.LienTypeID
                            INNER JOIN dbo.FileActions AS FA ON fa.FileID = l.FileID
                                                                AND fa.ActionDefID = 1184
                                                                AND fa.SentDate IS NOT NULL
                                                                AND fa.ReceivedDate IS NULL
                                                                AND fa.FileID = @FileID
    FOR           XML PATH('')
                     ,TYPE
        ).value('.', 'NVARCHAR(MAX)'), 1, 1, '')



SET @Query = 'DECLARE @FileID INT = 528267  SELECT  ' + @cols + '
      FROM (select   lt.name as Name, ''LienNumber'' + CONVERT(VARCHAR, ROW_NUMBER() OVER ( ORDER BY SortOrder )) as RN
     FROM

         dbo.Lien AS L
                INNER JOIN dbo.LienType AS LT ON LT.LienTypeID = L.LienTypeID
                INNER JOIN dbo.FileActions AS FA ON fa.FileID = l.FileID
                                                    AND fa.ActionDefID = 1184
                                                    AND fa.SentDate IS NOT NULL
                                                    AND fa.ReceivedDate IS NULL
                                                              AND fa.FileID = @FileID

                                              ) x
                                             pivot (       MAX(name) FOR RN IN ( ' + @Cols + ' ) ) p'




EXEC (@query)

借助:Convert Rows to columns using 'Pivot' in SQL Server

2 个答案:

答案 0 :(得分:1)

你几乎就在那里。另一个查询副本,在ID上转动,然后再由fileID连接,应该可以解决问题。这是SQLFiddle

SELECT
   a.FileID
  ,a.Lien1
  ,b.LienID1
  ,a.Lien2
  ,b.LienID2
  ,a.Lien3
  ,b.LienID3
  ,a.Lien4
  ,b.LienID4
  ,a.Lien5
  ,b.LienID5
  ,a.Lien6
  ,b.LienID6
FROM (
  SELECT
      FileID
     ,[1] AS Lien1
     ,[2] AS Lien2
     ,[3] AS Lien3
     ,[4] AS Lien4
     ,[5] AS Lien5
     ,[6] AS Lien6
  FROM    ( SELECT
                  FileID
                 ,LienName
                 ,ROW_NUMBER() OVER ( ORDER BY LienID ) AS RN
        FROM      dbo.Lien AS L

        WHERE     FileID = 528267
      ) AS PivotSource
  PIVOT ( MAX(LienName) FOR RN IN ( [1], [2], [3], [4], [5], [6] ) ) piv1
  ) a
  INNER JOIN (
  SELECT
      FileID
     ,[1] AS LienID1
     ,[2] AS LienID2
     ,[3] AS LienID3
     ,[4] AS LienID4
     ,[5] AS LienID5
     ,[6] AS LienID6
  FROM    ( SELECT
                  FileID
                 ,LienID 
                 ,ROW_NUMBER() OVER ( ORDER BY LienID ) AS RN
        FROM      dbo.Lien AS L

        WHERE     FileID = 528267
      ) AS PivotSource
  PIVOT ( MAX(LienID) FOR RN IN ( [1], [2], [3], [4], [5], [6] ) ) piv1
  ) b ON a.FileID = b.FileID

答案 1 :(得分:0)

如果我理解你想要的输出是正确的,我认为这个查询应该这样做,请试一试:

SELECT
    Lien1   = MAX([1]), 
    LienID  = MAX([r1]),
    Lien2   = MAX([2]),
    LienID  = MAX([r2]), 
    Lien3   = MAX([3]),
    LienID  = MAX([r3]),
    Lien4   = MAX([4]),
    LienID  = MAX([r4]),
    Lien5   = MAX([5]),
    LienID  = MAX([r5]),
    Lien6   = MAX([6]),
    LienID  = MAX([r6])
FROM ( 
    SELECT
       lt.Name AS [LienName]
       ,LienID 
       ,ROW_NUMBER() OVER ( ORDER BY LienID ) AS RN1
        ,'r'+CAST(ROW_NUMBER() OVER ( ORDER BY LienID ) AS varchar(10)) AS RN2
    FROM Lien AS L
    INNER JOIN LienType AS LT ON LT.LienTypeID = L.LienTypeID
    WHERE FileID = 528267
    ) AS PivotSource 
PIVOT ( MAX(LienName) FOR RN1 IN ( [1], [2], [3], [4], [5], [6] ) ) as names
PIVOT ( MAX(LienID)   FOR RN2 IN ( [r1], [r2], [r3], [r4],[r5],[r6] ) ) as ids