Monday, May 16, 2016

How to calculate running total

How to calculate running total ?

create table store(Id int primary key,productname varchar(20), sales int)

insert into store values (101,'grapes', 4000),
(102,'apples', 10000),
(103,'carrot', 14000),
(104,'watermelon', 9000),
(105,'mangoes', 12000)

select * from  store

Below is the query to find the running total sales 


 select ID,  productname,sales
 ,(select sum(sales) from store 
 where ID <= S.ID)
 'Running Total'
from store S

No comments:

Post a Comment