如何为 SUM 列指定名称? [英] How can I assign a name to the SUM column?

查看:41
本文介绍了如何为 SUM 列指定名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为 SUM 列指定列名?

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

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

我收到此错误:

缺少对象或列名或空的.对于 SELECT INTO 语句,验证每列都有一个名称.为了其他语句,查找空别名名称.定义为 "" 或 [] 的别名是不允许.将别名更改为有效名称.

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.

我猜是因为包含 SUM 结果的列没有名称.

I guess because the column containing the results of SUM has not name.

推荐答案

首先没有 SQL-Server 2003.只有 2000、2005、2008(然后是 2008R2、2012 和最新的 2014).

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

至于名称-称为别名-您可以使用AS.这是标准的 SQL 语法:

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 ... 

但是 AS 是可选的,所以你也可以为没有它的列设置别名:

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

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

您也可以使用(专有,仅在 SQL-Server 中)alias = column 语法:

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

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

这篇关于如何为 SUM 列指定名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆