sql-server – SQL Server 08 – pivot – 更改列名

我有这个代码:

SELECT * FROM(
   SELECT A.Id,
          B.Note,
          C.Value,
          C.Ammount
   FROM Table1 A
   LEFT JOIN Table2 B ON A.Id = B.Id
   LEFT JOIN Table3 C ON B.Id = c.Id AND B.Name = C.Name
   INNER JOIN(
      SELECT Name, LName, AxValue, Code, Number
      FROM Table Ax
      Where (Code = 80 AND Name = 'Bo') AS D ON D.AxValue = C.Value)
)AS Node1
PIVOT(
   SUM(Ammount)
   FOR Value IN ([1], [2])
)AS Node2

结果是这样的

Id   Note   1    2
--------------------
01   ok    500  100

有没有办法用名称(Ex1,Ex2)而不是数字(1,2)重命名最后两列?

最佳答案 替换:

SELECT * FROM(
...

通过

SELECT Id, Note, [1] as Ex1, [2] as Ex2 FROM(
...

用你需要的列替换*总是更好.您可以使用as作为别名.

您可以在此处找到更多信息:Using Table Aliases

在column_别名下面:SELECT Clause

点赞