I am using SQLite and am trying to return the total of one column buy_price
in the column TOTAL
while at the same time returning all of the data. I do not want/need to group the data as I need to have the data in each returned row.
id date pool_name pool_id buy_price TOTAL
1 09/01/12 azp 5 20
2 09/02/12 mmp 6 10
3 09/03/12 pbp 4 5
4 09/04/12 azp 7 20
5 09/05/12 nyp 8 5 60
When I include something like SUM(buy_price) as TOTAL
it only returns one row. I need all rows returned along with the total of all buy_price
entries.
Using sum() it returns NULL. Using total() it returns 0.0. SQLite sum() function retrieves the sum value of an expression which is made up of more than one columns.
AVG() SyntaxThe SUM() function returns the total sum of a numeric column.
Syntax of SQLite SUM() Function Following is the syntax of the SQLite SUM() function to get the sum of values in a defined expression. Expression – Its column or expression which we used to calculate the sum of values in defined column or expression.
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; If you need to arrange the data into groups, then you can use the GROUP BY clause.
It sounds like this is what you are looking for:
select id,
dt,
pool_name,
pool_id,
buy_price,
(select sum(buy_price) from yourtable) total
from yourtable
see SQL Fiddle with Demo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With