SQL Server Forums Profile | Register | Active Topics | Members | Search | Forum FAQ Register Now and get your question answered!
 All Forums  SQL Server 2008 Forums  Transact-SQL (2008)  How to calculate a cumulative value ? New Topic  Reply to Topic  Printer Friendly
Author  Topic

paulnamroud
Starting Member

24 Posts

 Posted - 06/28/2012 :  11:55:05 Hello,I have a transaction table that contains 3 columns:- Date- Item #- Quantity SoldSo, I would like to know how can I build a SQL Statement that calculate the cumulative value of a "Quantity Sold / Total Quantity Sold" by Date & Item # as shown below!Date Item # Quantity Sold Cumulative Quantity Sold (--> This Column that I want)----------------------------------------------------------------------------------------------------2012-06-01 101 10 10 (= 0 + 10) 2012-06-02 101 15 25 (= 10 + 15) 2012-06-03 101 100 125 (= 10 + 15 + 100)2012-06-04 101 225 350 (= 10 + 15 + 100 + 225) 2012-06-05 102 50 50 (= 0 + 50) 2012-06-06 102 25 75 (= 50 + 25) 2012-06-07 103 100 100 (= 0 + 100)2012-06-08 101 50 400 (= 10 + 15 + 100 + 225 + 50) 2012-06-08 102 100 175 (= 50 + 25 + 100) 2012-06-12 101 20 420 (= 10 + 15 + 100 + 225 + 50 + 20) 2012-06-14 101 40 460 (= 10 + 15 + 100 + 225 + 50 + 20 + 40) 2012-06-14 104 70 70 (= 0 + 70) 2012-06-18 103 80 180 (= 0 + 100 + 80)2012-06-22 101 -140 320 (= 10 + 15 + 100 + 225 + 50 + 20 + 40 -140) -> Credit2012-06-22 103 400 580 (= 0 + 100 + 80 + 400)2012-06-22 102 -22 153 (= 50 + 25 + 100 - 22) -> CreditThank youPaul

nigelrivett
Flowing Fount of Yak Knowledge

United Kingdom
3328 Posts

 Posted - 06/28/2012 :  12:33:28 If that's all you want then declare @t table (dte datetime, id int, q money)insert @t select '20010101', 1, 10insert @t select '20010102', 1, 20insert @t select '20010103', 1, 30select id, dte, q, qtot = (select SUM(q) from @t t2 where t2.id = t1.id and t2.dte <= t1.dte) from @t t1But you mention Quantity Sold / Total Quantity Sold sodeclare @t table (dte datetime, id int, q money)insert @t select '20010101', 1, 10insert @t select '20010102', 1, 20insert @t select '20010103', 1, 30;with t1 as (select id, q = SUM(q) from @t group by id), t2 as (select id, dte, q, qtot = (select SUM(q) from @t t2 where t2.id = t1.id and t2.dte <= t1.dte) from @t t1)select t2.dte, t2.q, t2.q/t1.qfrom t1join t2 on t1.id = t1.id==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.

visakh16
Very Important crosS Applying yaK Herder

India
47099 Posts

 Posted - 06/28/2012 :  16:00:03 see scenario 1http://visakhm.blogspot.com/2010/01/multipurpose-apply-operator.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
Topic
 New Topic  Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC