计算 sum() 两个别名命名的列 - 在 sql 中

发布于 2024-09-27 02:53:44 字数 401 浏览 1 评论 0原文

计算查询中声明的两个临时列名称的 sum() - 在 SQL

stud 表中只有两列 m1,m2。 Total 和total1 作为临时名称给出。

select 
   m1, m2, 
   SUM(m1) + SUM(m2) as Total,
   SUM(m1) + SUM(m2) as Total1 
from 
   stud 
group by 
   m1, m2

如何将 grandtotal 计算为 sum(total)+sum(total1) 并将列名称声明为要执行的查询的临时名称。

cte 不支持重复的列名?

如何利用它来支持重复的列名

To calculate sum() of two temp column names declared in query - in SQL

stud table has only two columns m1,m2. total and total1 is given as temp name.

select 
   m1, m2, 
   SUM(m1) + SUM(m2) as Total,
   SUM(m1) + SUM(m2) as Total1 
from 
   stud 
group by 
   m1, m2

How to calculate grandtotal as sum(total)+sum(total1) with the column name declared as temp name for the query to execute.

With cte dosn't support duplicate column names?

How to make use of it to support duplicate columnname

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-10-04 02:53:44

您不能直接执行此操作 - 您需要使用类似 CTE(通用表表达式)之类的东西 - 像这样:

;WITH sums AS 
(
   SELECT
      m1, m2, 
      SUM(m1) + SUM(m2) as Total,
      SUM(m1) + SUM(m2) as Total1 
   FROM
      dbo.stud 
   GROUP BY
      m1, m2
)
SELECT 
   m1, m2, 
   total, total1, 
   total+total1 AS 'GrandTotal' 
FROM 
   sums

这适用于 SQL Server 2005 及更高版本(以及支持 CTE 的其他一些数据库系统 - 这是一个 ANSI 标准)。

You can't do it directly - you need to use something like a CTE (Common Table Expression) - like this:

;WITH sums AS 
(
   SELECT
      m1, m2, 
      SUM(m1) + SUM(m2) as Total,
      SUM(m1) + SUM(m2) as Total1 
   FROM
      dbo.stud 
   GROUP BY
      m1, m2
)
SELECT 
   m1, m2, 
   total, total1, 
   total+total1 AS 'GrandTotal' 
FROM 
   sums

This works in SQL Server 2005 and newer (and also in some other database systems that support CTE's - which is an ANSI standard).

清风疏影 2024-10-04 02:53:44
select convert(int, first)+ convert(int,second) as total from test1 

在第一个和第二个字段中,字段数据类型为 nvarchar ,如果字段为整数,则为

select first+second as total from test1 , test1

表名称。

select convert(int, first)+ convert(int,second) as total from test1 

in this first and second is field datatype is nvarchar , and if fields are in integer then

select first+second as total from test1 , test1

is table name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文