2013-12-11 58 views
2

我有一個查詢下面,並需要有字段[Cmp-Goal-RF-148](這是pivoted成一列) - 我需要的列標題除[Cmp-Goal-RF-148]之外,所以我想我需要以別名。這樣做會拋出一個錯誤:([Cmp-Goal-RF-148] AS 'Ghost')。我錯過了什麼?向數據透視SQL查詢中的字段名稱添加別名

select * 
from 
(
    select EmpRvwPdDtl.Emp, EmpRvwPdDtl.Rvwr, 
    EmpRvwPdDtl.RvwItm, 
    CAST(EmpRvwPdDtl.RvwItmCom as VARCHAR(MAX)) as comment 
    from EmpRvwPdDtl 
    inner join EmpRvwPd 
    on (EmpRvwPd.Emp=EmpRvwPdDtl.Emp) 
    where EmpRvwPdDtl.RvwItmCom is not null 
    AND EmpRvwPd.Sup='RM04' 
) as s 
PIVOT 
(
    MAX(comment) for RvwItm in ([Cmp-Goal-RF-148]) 
) as pvit 

回答

2

您將添加別名在最後的SELECT列表:

select Emp, Rvwr, 
    [Cmp-Goal-RF-148] as Ghost -- alias goes here 
from 
(
    select EmpRvwPdDtl.Emp, EmpRvwPdDtl.Rvwr, 
    EmpRvwPdDtl.RvwItm, 
    CAST(EmpRvwPdDtl.RvwItmCom as VARCHAR(MAX)) as comment 
    from EmpRvwPdDtl 
    inner join EmpRvwPd 
    on (EmpRvwPd.Emp=EmpRvwPdDtl.Emp) 
    where EmpRvwPdDtl.RvwItmCom is not null 
    AND EmpRvwPd.Sup='RM04' 
) as s 
PIVOT 
(
    MAX(comment) for RvwItm in ([Cmp-Goal-RF-148]) 
) as pvit 
+0

這就是BRILLIAN,謝謝。在上面的查詢中有沒有辦法使用ORDER BY? – user2969966

+0

@ user2969966您將訂單作爲「pvit」別名後面的最後一行 – Taryn