2015-11-03 56 views
4

我有這樣的代碼:SQL Server的08 - 支點 - 更改欄目名稱

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)?

回答

1

替換:

SELECT * FROM(
... 

通過

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

它始終是更好的,你需要的列替換*。你可以用as將它們別名。

你可以在這裏找到更多的信息:column_ aliasUsing Table Aliases

在這裏:SELECT Clause

0

試試這個:

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 

而不是使用*你需要指定列的名稱。

+0

當我運行這個我得到的錯誤:多部分標識符「A.ID」無法綁定。 (對於其他別名也是如此) – SpB