Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Sum, it is possible

Author  Topic 

augustocolom
Starting Member

4 Posts

Posted - 2010-09-09 : 10:51:17
Hello, I wonder if you can bring the sum on a select,
for example:

Column Column right
25 25
25 50
25 75
25 100

it is possible?

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2010-09-09 : 13:36:00
A running total can be done in SQL, but you need to have a column that gives the ordering.

CREATE TABLE SillyStuff
(silly_seq INTEGER NOT NULL PRIMARY KEY,
silly_value INTEGER NOT NULL);

SELECT silly_seq, silly_value,
(SELECT SUM(S2.silly_value)
FROM SillyStuff AS S2
WHERE S1.silly_seq <= S2.silly_seq) AS running_tot
FROM SillyStuff AS S1;

Performance is terrible. Google the "SUM() OVER(RANGE..)" function in Standard SQL to see a better way to do this in other products.



--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page
   

- Advertisement -