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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Balance on the fly

Author  Topic 

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-07 : 02:48:32

Hi

I need your help again.

I would like to make this statement below to be changed so that the Balance is calculated on the fly and not inserted into the table. I would like it to be part of a view statement. Currently the Balance is a field on the table and updated but this makes me to run the statement everytime I want to see the Balance.

UPDATE b
SET b.Balance=b.Total + ISNULL(c.PrevBalance,0)
FROM BillingTransactions2 b
OUTER APPLY (SELECT SUM(Total) AS PrevBalance
FROM BillingTransactions2
WHERE UserIndex =b.UserIndex
AND BillingTransactionIndex<b.BillingTransactionIndex) c


Thank you for your kind help

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-07 : 03:42:06
[code]SELECT b.Total + ISNULL(c.PrevBalance,0) AS Balance,otherfields
FROM BillingTransactions2 b
OUTER APPLY (SELECT SUM(Total) AS PrevBalance
FROM BillingTransactions2
WHERE UserIndex =b.UserIndex
AND BillingTransactionIndex<b.BillingTransactionIndex) c[/code]
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-07 : 04:57:00
quote:
Originally posted by visakh16

SELECT b.Total + ISNULL(c.PrevBalance,0) AS Balance,otherfields
FROM BillingTransactions2 b
OUTER APPLY (SELECT SUM(Total) AS PrevBalance
FROM BillingTransactions2
WHERE UserIndex =b.UserIndex
AND BillingTransactionIndex<b.BillingTransactionIndex) c




I will like to add the above statement to the one below
SELECT DISTINCT TOP (100) PERCENT
(SELECT COUNT(*) AS Expr1
FROM dbo.BillsSections
WHERE (BillIndex = a.BillIndex) AND (Type <> 7) AND (BillSectionIndex > a.BillSectionIndex)) AS IsLast,
(SELECT SUM(Charge) AS Expr1
FROM dbo.BillsSections AS BillsSections_2
WHERE (BillIndex = a.BillIndex)) AS Sums,
(SELECT SUM(Charge) AS Expr1
FROM dbo.BillsSections AS BillsSections_1
WHERE (BillIndex = a.BillIndex) AND (Type = 7)) AS Tax, a.BillIndex, a.BillSectionIndex, a.Type, a.Charge, a.Info1, a.Info2, a.Info3, a.Remark,
b.BillStartDate, b.BillEndDate, b.CreateDate AS BillCreateDate, c.UserID, c.GroupName, c.UserPaymentBalance, d.UserIndex, d.FirstName,
d.MiddleName, d.LastName, d.Company, d.Address1, d.Address2, d.City, d.State, d.Country, d.Zip, d.PhoneHome, d.PhoneWork, d.PhoneFax, d.Email,
d.CreateDate, d.LastModify, d.NewUser, d.LastCharge, d.TotalCharge, d.CustomInfo4, d.Comments, e.ServiceName,
dbo.AccountTypes.AccountName
FROM dbo.AccountTypes INNER JOIN
dbo.Users AS c ON dbo.AccountTypes.AccountIndex = c.AccountIndex RIGHT OUTER JOIN
dbo.Bills AS b RIGHT OUTER JOIN
dbo.BillsSections AS a ON b.BillIndex = a.BillIndex ON c.UserIndex = b.UserIndex LEFT OUTER JOIN
dbo.UserDetails AS d ON c.UserIndex = d.UserIndex LEFT OUTER JOIN
dbo.Services AS e ON a.Info1 = e.ServiceIndex
WHERE (b.BillType = 1) AND (a.Type <> 7)
ORDER BY a.BillIndex, a.BillSectionIndex
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-07 : 05:33:43
how do you want to format your result set?
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-07 : 05:37:12
quote:
Originally posted by visakh16

how do you want to format your result set?



The Balance showing last
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 04:14:57
I really need help with this. Anybody with an answer PLEASE
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-08 : 05:29:55
How can you relate the current query to the one i gave? do you have a field in Biling Transactions which can be linked on current query?
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 05:38:06
quote:
Originally posted by visakh16

How can you relate the current query to the one i gave? do you have a field in Biling Transactions which can be linked on current query?



Yes UserIndex and BillIndex
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-08 : 05:56:52
[code]SELECT DISTINCT TOP (100) PERCENT
(SELECT COUNT(*) AS Expr1
FROM dbo.BillsSections
WHERE (BillIndex = a.BillIndex) AND (Type <> 7) AND (BillSectionIndex > a.BillSectionIndex)) AS IsLast,
(SELECT SUM(Charge) AS Expr1
FROM dbo.BillsSections AS BillsSections_2
WHERE (BillIndex = a.BillIndex)) AS Sums,
(SELECT SUM(Charge) AS Expr1
FROM dbo.BillsSections AS BillsSections_1
WHERE (BillIndex = a.BillIndex) AND (Type = 7)) AS Tax, a.BillIndex, a.BillSectionIndex, a.Type, a.Charge, a.Info1, a.Info2, a.Info3, a.Remark,
b.BillStartDate, b.BillEndDate, b.CreateDate AS BillCreateDate, c.UserID, c.GroupName, c.UserPaymentBalance, d.UserIndex, d.FirstName,
d.MiddleName, d.LastName, d.Company, d.Address1, d.Address2, d.City, d.State, d.Country, d.Zip, d.PhoneHome, d.PhoneWork, d.PhoneFax, d.Email,
d.CreateDate, d.LastModify, d.NewUser, d.LastCharge, d.TotalCharge, d.CustomInfo4, d.Comments, e.ServiceName, ,
dbo.AccountTypes.AccountName,tmp.BalanceFROM dbo.AccountTypes INNER JOIN
dbo.Users AS c ON dbo.AccountTypes.AccountIndex = c.AccountIndex RIGHT OUTER JOIN
dbo.Bills AS b RIGHT OUTER JOIN
dbo.BillsSections AS a ON b.BillIndex = a.BillIndex ON c.UserIndex = b.UserIndex LEFT OUTER JOIN
dbo.UserDetails AS d ON c.UserIndex = d.UserIndex LEFT OUTER JOIN
dbo.Services AS e ON a.Info1 = e.ServiceIndex
INNER JOIN (SELECT b.UserIndex,b.BillIndex,b.Total + ISNULL(c.PrevBalance,0) AS Balance
FROM BillingTransactions2 b
OUTER APPLY (SELECT SUM(Total) AS PrevBalance
FROM BillingTransactions2
WHERE UserIndex =b.UserIndex
AND BillingTransactionIndex<b.BillingTransactionIndex) c

)tmp
ON tmp.UserIndex=c.UserIndex
AND tmp.BillIndex=a.BillIndex
WHERE (b.BillType = 1) AND (a.Type <> 7)
ORDER BY a.BillIndex, a.BillSectionIndex[/code]
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 06:00:45
I get the errors below when I the statments

Msg 102, Level 15, State 1, Line 13
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'tmp'.
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 06:04:58
New errors below

Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'INNER'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'tmp'.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-08 : 06:17:13
quote:
Originally posted by tkotey

New errors below

Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'INNER'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'tmp'.



i've missed a space b/w field name and from . Are you sure you've included it?
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 06:32:51
quote:
Originally posted by visakh16

quote:
Originally posted by tkotey

New errors below

Msg 156, Level 15, State 1, Line 14
Incorrect syntax near the keyword 'INNER'.
Msg 102, Level 15, State 1, Line 26
Incorrect syntax near 'tmp'.



i've missed a space b/w field name and from . Are you sure you've included it?



No I did not. I just took it the way it was. I was wondering why there was no from
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 06:37:04
Fixed it. Just testing now
Go to Top of Page

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 10:00:14
Yes it works. Thanks visakh16. Your code did the trick. Do you know any place on the net or books I can buy to increase my SQL skills?

Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-07-08 : 10:31:16
>>Do you know any place on the net ... to increase my SQL skills?

http://www.sqlteam.com




elsasoft.org
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-07-08 : 11:58:28
quote:
Originally posted by tkotey

Yes it works. Thanks visakh16. Your code did the trick. Do you know any place on the net or books I can buy to increase my SQL skills?




If you need to Learn SQL:
http://www.sql-tutorial.net/
http://www.firstsql.com/tutor.htm
http://www.w3schools.com/sql/default.asp

The primary Microsoft SQL Server Reference:
SQL Server 2005 Books Online
http://msdn2.microsoft.com/en-us/library/ms130214.aspx




CODO ERGO SUM
Go to Top of Page
   

- Advertisement -