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)
 Need help with view query

Author  Topic 

tkotey
Yak Posting Veteran

75 Posts

Posted - 2008-07-08 : 05:35:47
[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]




I will like to add the above statement to the one below

[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
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[/code]

The balance should show at the end in the output

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-08 : 05:37:08
how should they be linked? whats the linking column?
Go to Top of Page
   

- Advertisement -