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.
| Author |
Topic |
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2008-07-07 : 02:48:32
|
HiI 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 bSET b.Balance=b.Total + ISNULL(c.PrevBalance,0)FROM BillingTransactions2 bOUTER 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,otherfieldsFROM BillingTransactions2 bOUTER APPLY (SELECT SUM(Total) AS PrevBalance FROM BillingTransactions2 WHERE UserIndex =b.UserIndex AND BillingTransactionIndex<b.BillingTransactionIndex) c[/code] |
 |
|
|
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,otherfieldsFROM BillingTransactions2 bOUTER 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 belowSELECT 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.AccountNameFROM 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.ServiceIndexWHERE (b.BillType = 1) AND (a.Type <> 7)ORDER BY a.BillIndex, a.BillSectionIndex |
 |
|
|
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? |
 |
|
|
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 |
 |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2008-07-08 : 04:14:57
|
I really need help with this. Anybody with an answer PLEASE |
 |
|
|
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? |
 |
|
|
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 |
 |
|
|
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.ServiceIndexINNER JOIN (SELECT b.UserIndex,b.BillIndex,b.Total + ISNULL(c.PrevBalance,0) AS BalanceFROM BillingTransactions2 bOUTER APPLY (SELECT SUM(Total) AS PrevBalance FROM BillingTransactions2 WHERE UserIndex =b.UserIndex AND BillingTransactionIndex<b.BillingTransactionIndex) c)tmpON tmp.UserIndex=c.UserIndexAND tmp.BillIndex=a.BillIndexWHERE (b.BillType = 1) AND (a.Type <> 7)ORDER BY a.BillIndex, a.BillSectionIndex[/code] |
 |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2008-07-08 : 06:00:45
|
| I get the errors below when I the statmentsMsg 102, Level 15, State 1, Line 13Incorrect syntax near ','.Msg 102, Level 15, State 1, Line 26Incorrect syntax near 'tmp'. |
 |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2008-07-08 : 06:04:58
|
| New errors belowMsg 156, Level 15, State 1, Line 14Incorrect syntax near the keyword 'INNER'.Msg 102, Level 15, State 1, Line 26Incorrect syntax near 'tmp'. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-07-08 : 06:17:13
|
quote: Originally posted by tkotey New errors belowMsg 156, Level 15, State 1, Line 14Incorrect syntax near the keyword 'INNER'.Msg 102, Level 15, State 1, Line 26Incorrect syntax near 'tmp'.
i've missed a space b/w field name and from . Are you sure you've included it? |
 |
|
|
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 belowMsg 156, Level 15, State 1, Line 14Incorrect syntax near the keyword 'INNER'.Msg 102, Level 15, State 1, Line 26Incorrect 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 |
 |
|
|
tkotey
Yak Posting Veteran
75 Posts |
Posted - 2008-07-08 : 06:37:04
|
| Fixed it. Just testing now |
 |
|
|
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? |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
|
|
|
|
|
|
|