SQL Server 08 - pivot - Change Column Name

I have this code:

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

and the result is something like this

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

Is it possible to rename the last two columns with the names (Ex1, Ex2) instead of numbers (1,2)?

+4
source share
2 answers

Replace:

SELECT * FROM(
...

By

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

It is always better to replace *with the necessary columns. You can use them with as.

Further information can be found here: Using Table Aliases

And here under column_ alias: SELECT clause

+1
source

Try the following:

SELECT Id, Note, [1] as Ex1, [2] as Ex2 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

Instead of using *you need to specify the name of your column.

0

Source: https://habr.com/ru/post/1614267/


All Articles