Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

sum

hsqldb

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

like image 472
ThE_-_BliZZarD Avatar asked Apr 13 '10 08:04

ThE_-_BliZZarD


People also ask

How do I sum all rows in a column in SQL?

If you need to add a group of numbers in your table you can use the SUM function in SQL. This is the basic syntax: SELECT SUM(column_name) FROM table_name; The SELECT statement in SQL tells the computer to get data from the table.

How do I combine column values in one column in SQL?

Query: SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME FROM demo_table; Output: Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(' ') in CONCAT() function.

How do I sum a column with the same ID in SQL?

To sum rows with same ID, use the GROUP BY HAVING clause.

How do I combine multiple rows of data into one row in SQL?

You can concatenate rows into single string using COALESCE method. This COALESCE method can be used in SQL Server version 2008 and higher. All you have to do is, declare a varchar variable and inside the coalesce, concat the variable with comma and the column, then assign the COALESCE to the variable.


2 Answers

SELECT SUM(size) AS overallSize FROM table WHERE bla = 5;
like image 131
Michael Mrozek Avatar answered Oct 27 '22 14:10

Michael Mrozek


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

SELECT SUM(SIZE)
FROM Table
WHERE bla = '5'
like image 43
Paddy Avatar answered Oct 27 '22 14:10

Paddy