Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite: Get Total/Sum of Column

Tags:

sql

sqlite

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.

like image 743
user1650361 Avatar asked Sep 05 '12 22:09

user1650361


People also ask

How do I sum a column in SQLite?

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.

How do you get the total sum of a column in SQL?

AVG() SyntaxThe SUM() function returns the total sum of a numeric column.

How can I sum a column in SQLite in Android?

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.

How do I sum a row 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; If you need to arrange the data into groups, then you can use the GROUP BY clause.


1 Answers

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

like image 65
Taryn Avatar answered Nov 02 '22 20:11

Taryn