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

随声附和 提交于 2019-11-27 06:38:43

问题


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

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

Thank you in advance :)


回答1:


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



回答2:


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

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



回答3:


Are you looking for:

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

You can also (in MSSQL)

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


来源:https://stackoverflow.com/questions/2628159/sql-add-up-all-row-values-of-one-column-in-a-singletable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!