2013-12-11 2 views
2

아래 쿼리가 있고 [Cmp-Goal-RF-148] 필드가 필요합니다 (열이 피벗 됨) - 열 제목이 [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

관련 문제