Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How Can I add Running Balance Column

Posted on 2012-03-12
1
Medium Priority
?
487 Views
Last Modified: 2012-03-12
I am using below mentioned query to get result from database. Can some one help me to add running balance in this query.

Thanks in advance.

-----

Select * from
(Select Disc='PON-'+Isnull(PON,'')+' Vendor Invoice # '+ BN + ' Receipt Date '+ Convert(Varchar(12),Date),
       a.Date,b.VendorName,SNo=Row_Number() Over (Partition by BN Order by bn,a.Transno), c.FullItem,Qty,Rate,Debit=0, Credit=Amt
from mtPurchased a
join mtVendor b on a.VendorCode=b.Transno
join mtIPUOM c on a.IC=c.Transno
Where b.VendorName='Arman Autos' and a.del=0
--Order by bn,a.Transno
Union All
Select Disc='Paid Through ' + c.OS + ' ' + 'Paid To ' + b.VendorName + ' On ' +Convert(Varchar(12),Pdate) + ' Ref No. ' +a.ACRefNo + 'Amount Rs. ' + Convert(Varchar(10),sum(amt)),
            PDate,b.VendorName, Sno=Row_Number() Over (Partition by ACRefNo Order by Pdate,ACRefNo), AcRefNo, Qty=0, Rate=0, Debit=Sum(Amt), Credit=0
from mtPayment a
join mtVendor b on a.VendorCode=b.Transno
join mtOppStaff c on a.OS=c.Transno
Where b.VendorName='Arman Autos'
group by c.OS, b.VendorName,a.PDate, a.ACRefNo
--Order by Pdate,ACRefNo
)x
order by date
0
Comment
Question by:Mehram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
jogos earned 2000 total points
ID: 37709207
How to running total see options  at
http://www.sqlteam.com/article/calculating-running-totals

Put option 1 in a CTE-sollution http://msdn.microsoft.com/en-us/library/ms175972.aspx
Don't foget to attend the TODO's mentioned.

One thing I didn't catch is how your running total is for outcome of the union or for each part of the union -> handle in the subquery cte1 vs cte2

;WITH CTE () as (  , , )   -- TODO columnlist
(Select Disc='PON-'+Isnull(PON,'')+' Vendor Invoice # '+ BN + ' Receipt Date '+ Convert(Varchar(12),Date),
       a.Date,b.VendorName,SNo=Row_Number() Over (Partition by BN Order by bn,a.Transno), c.FullItem,Qty,Rate,Debit=0, Credit=Amt 
from mtPurchased a
join mtVendor b on a.VendorCode=b.Transno
join mtIPUOM c on a.IC=c.Transno
Where b.VendorName='Arman Autos' and a.del=0
--Order by bn,a.Transno
Union All
Select Disc='Paid Through ' + c.OS + ' ' + 'Paid To ' + b.VendorName + ' On ' +Convert(Varchar(12),Pdate) + ' Ref No. ' +a.ACRefNo + 'Amount Rs. ' + Convert(Varchar(10),sum(amt)),
            PDate,b.VendorName, Sno=Row_Number() Over (Partition by ACRefNo Order by Pdate,ACRefNo), AcRefNo, Qty=0, Rate=0, Debit=Sum(Amt), Credit=0
from mtPayment a
join mtVendor b on a.VendorCode=b.Transno
join mtOppStaff c on a.OS=c.Transno
Where b.VendorName='Arman Autos' 
group by c.OS, b.VendorName,a.PDate, a.ACRefNo
--Order by Pdate,ACRefNo
) 
select ct1.*
 , (select sum(cte2. )   -- TODO how is your balance-constructed
     from cte as ct2
     where ct2. = ct1.   --- TODO  the whole join-condition 
     and ct2.date <= ct1.date
     and ct2.SNo <= ct1.SNo
   ) as runningBalance
from CTE as cte1
order by date,SNo

Open in new window

0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question