gpt4 book ai didi

sql - 在 Pivot SQL 查询中为字段名称添加别名

转载 作者:行者123 更新时间:2023-12-05 03:15:08 25 4
gpt4 key购买 nike

我在下面有一个查询,需要有字段[Cmp-Goal-RF-148](它被旋转成一列)-我需要列标题是 [Cmp-Goal-RF-148],所以我想我需要给它起别名。这样做会引发错误:([Cmp-Goal-RF-148] AS 'Ghost')。我错过了什么?

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

最佳答案

您将在最终的 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

关于sql - 在 Pivot SQL 查询中为字段名称添加别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20522359/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com