sql - Adding first two rows result as a second row then addition of first three rows result as a third row and so on -


i have table in sql serevr,which has 1 column , storing integer values.

ex : columndata


   100     150     20     25     300 

now using data want result shown below.

columndata newcolumn


   100        100     150        250     20         270     25         295     300        595 

so in output newcolumn added logic i.e first row data firstrow,then first 2 rows addition result appears in second row,then first 3 rows addition result appears in third row on...

could 1 please provide me query how result.

thanks in advance,

phani kumar.

assuming have column can order data can compute running total either using windowed aggregate function (this works in sql server 2012+) or self join (which works in version). if don't have column order can't done in deterministic way @ all.

-- sample table: create table t (id int identity(1,1), columndata int) insert t values (100),(150),(20),(25),(300)  -- query 1 using windowed aggregate select columndata, sum(columndata) on (order id)  newcolumn t order id  -- query 2 using self-join     select t1.columndata, sum(t2.columndata)  newcolumn t t1 join t t2 on t2.id <= t1.id group t1.id, t1.columndata order t1.id 

sample sql fiddle


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -