2009-12-22 3 views
3

У меня есть КТР следующимОСИ на общей таблице Выражение

WITH details 
     AS (SELECT FldId 
        ,Rev 
        ,Words 
        ,row_number() OVER (PARTITION BY FldId ORDER BY Rev DESC) AS rn 
      FROM WorkItemLongTexts 
      WHERE ID = 2855 
      ) 
    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 ; 

Вышеприведенные возвращает следующий выходной

ReferenceName | FldId  | Rev  | Words 
Description   52   2   Description here 
Objectives   10257   2   Objectives here 
Specification  10258   6   Specification here 
Requirements   10259   6   Requirements here 

Я хочу, чтобы применить PIVOT (или то, что лучший вариант), так что я может получить выход следующего

Description   |  Objectives  | Specification  | Requirements 

описания здесь                 Цели здесь                 Спецификация здесь                 Требования здесь

Pls. предлагать.

Благодаря

ответ

3
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 ; 

ИЛИ:

-- 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

получения ошибки «тип данных Операнд NTEXT является недопустимым для максимального оператора.» – stackoverflowuser

+0

Можете ли вы наложить на nvarchar (max)? ntext устарел. –

+0

'... THEN CAST ([Words] AS nvarchar (max)') вместо 'THEN [Words]'; или nvarchar (4000), если 4000 достаточно –

1

ли что-то вроде:

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 
; 
3

Вы делаете это:

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 

Или вы можете добавить его в КТР, как это:

;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 
Смежные вопросы