SQL - 将单表中一列的所有行值相加

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2628159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 05:59:17  来源:igfitidea点击:

SQL - Add up all row-values of one column in a singletable

sqlsumhsqldb

提问by ThE_-_BliZZarD

I've got a question regarding a SQL-select-query: The table contains several columns, one of which is an Integer-column called "size" - the task I'm trying to perform is query the table for the sum of all rows (their values), or to be more exact get a artifical column in my ResultSet called "overallSize" which contains the sum of all "size"-values in the table. Preferable it would be possible to use a WHERE-clause to add only certain values ("WHERE bla = 5" or something similar).

我有一个关于 SQL-select-query 的问题:该表包含几列,其中一个是一个名为“size”的整数列 - 我试图执行的任务是查询表中所有的总和行(它们的值),或者更确切地说,在我的 ResultSet 中获得一个名为“overallSize”的人工列,它包含表中所有“大小”值的总和。最好可以使用 WHERE 子句仅添加某些值(“WHERE bla = 5”或类似的东西)。

The DB-engine is HSQLDB (HyperSQL), which is compliant to SQL2008.

数据库引擎是HSQLDB(HyperSQL),符合SQL2008。

Thank you in advance :)

先感谢您 :)

采纳答案by Michael Mrozek

SELECT SUM(size) AS overallSize FROM table WHERE bla = 5;

回答by Paddy

It's not as simple as this, is it?

事情不是这么简单的,是吗?

SELECT SUM(SIZE)
FROM Table
WHERE bla = '5'

回答by cjk

Are you looking for:

您是否正在寻找:

SELECT SUM(Size) FROM MyTable WHERE bal = '5'

You can also (in MSSQL)

你也可以(在 MSSQL 中)

SELECT Size, COl1, COl2 FROM MyTable WHERE bla ='5' COMPUTE SUM(Size)