How can I assign a name to the SUM column?

前端 未结 3 403
离开以前
离开以前 2020-12-06 22:00

how can I assign a column name to the SUM column ?

i.e.

select OwnerUserId, SUM(PostScore)
INTO Experts
from ...

I get this error:

相关标签:
3条回答
  • 2020-12-06 22:09

    First of all there's no SQL-Server 2003. Only 2000, 2005, 2008 (and then 2008R2, 2012 and the latest 2014).

    As for the name - called alias - you can use AS. This is the standard SQL syntax:

    SELECT OwnerUserId, SUM(PostScore) AS PostScoreSum
    INTO Experts 
    FROM ... 
    

    But AS is optional, so you can also alias a column without it:

    SELECT OwnerUserId, SUM(PostScore)  PostScoreSum
    INTO Experts 
    FROM ... 
    

    You can also use the (proprietary, only in SQL-Server) alias = column syntax:

    SELECT OwnerUserId, PostScoreSum = SUM(PostScore)  
    INTO Experts 
    FROM ... 
    
    0 讨论(0)
  • 2020-12-06 22:28

    You can alias any column, computed or not by using the AS keyword:

    SELECT OwnerUserId AS MyUserId, SUM(PostScore) As Score
    INTO Experts
    from ...
    
    0 讨论(0)
  • 2020-12-06 22:32
    select OwnerUserId, SUM(PostScore) as mySum
    INTO Experts
    from ...
    

    or

    select OwnerUserId, SUM(PostScore) [mySum]
    INTO Experts
    from ...
    
    0 讨论(0)
提交回复
热议问题