May 31, 2006 at 3:21 pm
I need to make activity report like below:
CUSTOMER : mr. X
description amount Balance
31 jan 2006 beggining balance 100
05 feb 2006 sales 200 300
08 feb 2006 sales again 500 800
12 feb 2006 next sales 100 900
15 feb 2006 another sales 600 1500
i have table for retrieve beggining balance like below:
customer beg balance
mr. x 100
mr. yy 250
so on....
and table transaction...
customer date description amount
mr. x 05 feb 2006 sales 200
mr. x 08 feb 2006 sales again 500
mr. x 12 feb 2006 next sales 100
mr. x 15 feb 2006 another sales 600
mr. yy 05 mar 2006 2 piece 200
mr. yy 08 apr 2006 just one piece 500
so on....
please give some advice, how do i calculate balance with t-sql for activity report
Thank's dan Best Regards,
Sandi Antono
May 31, 2006 at 4:07 pm
Here's one way, using pure SQL. I've had to guess at your table structures and some other things.
c.FullName
sp.StartDate Date
cast('Opening Balance' as varchar(1000)) Description
cob.Amount
cob.Amount Balance
CustOpeningBal cob
SalesPeriod sp
cob.SalesPeriodID = sp.SalesPeriodID
Customer c
cob.CustID = c.CustID
sp.StartDate = '2006-01-31 00:00:00'
all
c.FullName, t.Date, t.Description, t.Amount, sum(t2.amount)+cob.amount
Transactions t
Customer c
CustOpeningBal cob on cob.CustID = t.CustID
SalesPeriod sp on cob.SalesPeriodID = sp.SalesPeriodID
and t.Date >= sp.StartDate
join Transactions t2 on t2.CustID = t.CustID
sp.StartDate = '2006-01-31 00:00:00'
by c.FullName, t.Date, t.Description, t.Amount
by FullName, case Description when 'Opening Balance' then 0 else 1 end, Date
If you could supply more info about your tables, it would help.
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 31, 2006 at 4:11 pm
This will work but I don't know how it will perform with a large number of rows in the transaction table.
DECLARE @begbal TABLE (customer varchar(20), balance int, baldate datetime)
insert @begbal VALUES ('mr. x', 100, '31 Jan 2006')
insert @begbal Values('mr. yy', 250, '31 Jan 2006')
DECLARE @transaction TABLE (customer varchar(20), trandate datetime, description varchar(20), amount int)
INSERT @transaction values('mr. x', '05 feb 2006', 'sales', 200)
INSERT @transaction values('mr. x', '08 feb 2006', 'sales again', 500)
INSERT @transaction values('mr. x', '12 feb 2006', 'next sales', 100)
INSERT @transaction values('mr. x', '15 feb 2006', 'another sales', 600)
INSERT @transaction values('mr. yy', '05 mar 2006', '2 piece', 200)
INSERT @transaction values('mr. yy', '08 apr 2006', 'just one piece', 500)
SELECT b.baldate, 'Beginning balance', null, balance
FROM @begbal b
WHERE b.customer = 'mr. x'
UNION ALL
SELECT t.trandate, t.description, t.amount, b.balance + x.runningbal
FROM @begbal b
inner join @transaction t
on b.customer = t.customer
INNER JOIN (select a.customer, a.trandate, sum(b.amount) runningbal
FROM @transaction a
INNER JOIN @transaction b
on b.trandate <= a.trandate
GROUP BY a.customer, a.trandate) x
ON t.customer = x.customer AND t.trandate = x.trandate
WHERE b.customer = 'mr. x'
ORDER BY 1
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply