2009-12-22 57 views
3

我有一個熱膨脹係數如下PIVOT對公共表表達式

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 ; 

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

得到錯誤 「操作數的數據類型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 

或者你可以把它添加到您的CTE,像這樣:

;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