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
 Query help

Author  Topic 

Joshrinn
Posting Yak Master

118 Posts

Posted - 2012-07-03 : 12:21:33
Hi,
I am preparing a report an I need a help on the query. There are three columns involved in particular to get this result. A column called 'amount' has a value of 300000, another column 'balance' and the third column is remaining balance. Now I need to populate the remaining balance column. The logic here is the first amount of remaining balance can be got by subtracting 'amount'-'balance'.eg
Amount=300000
Balance=30000
Remaining balance will be 270000. Now the second amount row for the remaining balance column will be populated by subtracting the 'balance' column and 'remaining balance' column until the 'remaining balance' column has a value of zero. For instance
Remaining balance after the above calculation is 2700000 and if the 'balance' column has a value of '3000'
'remaining balance' will be be 267000.please help how to attain this

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-03 : 13:49:44
For you to be able to do this, you need some way to define the ordering of the rows. Is there another column that will enable you to do that? Even though when you select from the table, the return result set looks like it is ordered in some fashion, that order is not guaranteed, hence the need for some systematic way of ordering the columns.
Go to Top of Page

Joshrinn
Posting Yak Master

118 Posts

Posted - 2012-07-03 : 15:00:47
Yes I have an ordering standard that I will follow. It will b on the basis of date an few other fields.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-03 : 15:32:44
quote:
Originally posted by Joshrinn

Yes I have an ordering standard that I will follow. It will b on the basis of date an few other fields.

Below are couple of ways in which you can do these computations. I am vague in the rules and the column names because I don't have the details on your table schema's.
------------------------------------------------------------
-- using a cte
------------------------------------------------------------
;WITH cte AS
(
SELECT *,COALESCE(Amount,0)-COALESCE(Balance,0) AS NewRemainingBalance
FROM YourTable
WHERE
YourOrderingColumnValue = 1
UNION ALL
SELECT
y.*, c.NewRemainingBalance - c.Balance
FROM
YourTable y
INNER JOIN cte c ON
c.YourOrderingColumnValue+1 = y.YourOrderingColumnValue
)
SELECT * FROM cte;

------------------------------------------------------------
-- using a subquery
------------------------------------------------------------
SELECT
y.*,
c.NewRemainingBalance
FROM
YourTable y
CROSS APPLY
(
SELECT SUM(COALESCE(c.Amount,0))-SUM(COALESCE(c.Balance,0)) AS NewRemainingBalance
FROM YourTable c
WHERE c.YourOrderingColumnValue <= y.YourOrderingColumnValue
) c;
Neither of these are going to be great from a performance perspective. If that becomes a problem, look up the quirky update approach - some details here: http://www.sqlservercentral.com/articles/T-SQL/68467/

(Unless of course, if you are already on SQL 2012, the expanded windowing function capabilities would allow you to do this easily).
Go to Top of Page

Joshrinn
Posting Yak Master

118 Posts

Posted - 2012-07-04 : 12:58:20
It is not giving me te desired result. Actually Is there a way where we can create a while loop ? In a way that the first time remaining balance result would be achieved by subtracting 300000-totalbalance column. Then it would subsequently subtract the next amount present in the totalbalance column until the value of remaining balance goes to zero. If I assign 300000 to one of the column which is always static there will be only two columns which I need to worry about. So if you could help me with this it would b great. Again thanks fr your above response
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-07-04 : 15:24:07
I am not surprised that the query did not work right out of the box. My SQL skills are not good enough to be able to write queries without seeing the DDL for the tables and some sample data. Hopefully someone else on the forum will be able to offer better suggestions.

Usually you will get better and accurate answers if you post sample data and DDL for creating and populating test tables. Brett's blog here might be a good guide: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
Go to Top of Page
   

- Advertisement -