PIVOT on Common Table Expression

前端 未结 3 1265
南旧
南旧 2020-12-18 04:28

I have a CTE as follows

WITH  details
        AS ( SELECT FldId
                   ,Rev
                   ,Words
                   ,row_number() OVER ( PAR         


        
相关标签:
3条回答
  • 2020-12-18 05:01

    You do this:

    SELECT
        FldId,
        [Description],
        [Objectives],
        [Specification],
        [Requirements]
    FROM (
        SELECT
            ReferenceName,
            FldId,
            REV,
            Words
        FROM CTE
        WHERE RowNumber = 1
    ) t
    PIVOT (
        MIN(Words)
        FOR ReferenceName IN ([Description], [Objectives], [Specification], [Requirements])
    ) PIV
    

    Or you can add it to your CTE, like this:

    ;WITH CTE2 AS (
        SELECT
            FldId,
            REV,
            [Description],
            [Objectives],
            [Specification],
            [Requirements],
            ROW_NUMBER() OVER (PARTITION BY FldId ORDER BY REV DESC) AS RowNumber
        FROM TBL
    PIVOT (
            MIN(Words)
            FOR ReferenceName IN ([Description], [Objectives], [Specification], [Requirements])
        ) PIV
    )
    
    SELECT
        FldId,
        REV,
        [Description],
        [Objectives],
        [Specification],
        [Requirements]
    FROM CTE2
    WHERE RowNumber = 1
    
    0 讨论(0)
  • 2020-12-18 05:06

    Do something like:

    with details as (...)
    , unpivotted as (select f.ReferenceName, Words 
      from details as d
      inner join Fields as f
      on f.FldId=d.FldId
      where d.rn =1)
    Select *
    from unpivotted 
    pivot 
    (max(Words) for Description in ([Objectives],[Specification],[Requirements]) p
    ;
    
    0 讨论(0)
  • 2020-12-18 05:08
    WITH  details
            AS ( SELECT FldId
                       ,Rev
                       ,Words
                       ,row_number() OVER ( PARTITION BY FldId ORDER BY Rev DESC ) AS rn
                 FROM   WorkItemLongTexts
                 WHERE  ID = 2855
               ),
          cte_1
            AS ( SELECT f.ReferenceName
                       ,d.FldId
                       ,Rev
                       ,Words
                 FROM   details AS d
                        INNER JOIN Fields AS f ON f.FldId = d.FldId
                 WHERE  d.rn = 1
               )
      SELECT  max(case [ReferenceName] WHEN 'Descripton' THEN [Words] ELSE NULL END) AS [Descripton]
             ,max(case [ReferenceName] WHEN 'Objectives' THEN [Words] ELSE NULL END) AS [Objectives]
             ,max(case [ReferenceName] WHEN 'Specification' THEN [Words] ELSE NULL END) AS [Specification]
             ,max(case [ReferenceName] WHEN 'Requirements' THEN [Words] ELSE NULL END) AS [Requirements]
      FROM    cte_1 ;
    

    OR:

      -- cte here as above
      SELECT  Description
             ,Objectives
             ,Specification
             ,Requirements
      FROM    cte_1 PIVOT ( max(Words) FOR ReferenceName IN ( Description,
                                                              Objectives,
                                                              Specification,
                                                              Requirements ) ) AS PivotTable
    
    0 讨论(0)
提交回复
热议问题