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 |
sn34
Starting Member
36 Posts |
Posted - 2010-10-15 : 02:35:55
|
HelloI have following table named "Sales": ------id------Doc_type------amount------remiaining_amount <---- i whant to calculate this 1------invoice------400--------------- 0 <--- this 0 because invoice is 400 and sum of next payments(2 and 4) is 250+150=400, remaining=400-4002------payment------250---------------03------invoice------100---------------50 <--- remaining is 50 because 1 invoice is 0 and next payment is 50 and remaining=100-504------payment------150--------------0 5------payment------50---------------0 |
|
kunal.mehta
Yak Posting Veteran
83 Posts |
Posted - 2010-10-15 : 03:52:00
|
seem to have problem in the design of the table |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-10-15 : 05:11:14
|
[code]declare @sales table( id int, doc_type varchar(10), amount int)insert into @sales (id, doc_type, amount)select 1, 'invoice', 400 union allselect 2, 'payment', 250 union allselect 3, 'invoice', 100 union allselect 4, 'payment', 150 union allselect 5, 'payment', 50 select s.id, s.doc_type, s.amount, remaining_amt = case when doc_type = 'invoice' then case when s.amount + isnull(i.inv_amt, 0) < p.amount then 0 when s.amount + isnull(i.inv_amt, 0) - p.amount < s.amount then s.amount + isnull(i.inv_amt, 0) - p.amount else s.amount end else 0 endfrom @sales s cross apply ( select amount = sum(amount) from @sales x where doc_type = 'payment' ) p cross apply ( select inv_amt = sum(amount) from @sales x where x.id < s.id and doc_type = 'invoice' ) i/*id doc_type amount remaining_amt ----------- ---------- ----------- ------------- 1 invoice 400 02 payment 250 03 invoice 100 504 payment 150 05 payment 50 0(5 row(s) affected)*/[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
sn34
Starting Member
36 Posts |
Posted - 2010-10-16 : 11:19:51
|
Thank you this works fine, i whant to add one column "cust_id" adn calculate remaining for evri customer like that:/*id cust_id doc_type amount remaining_amt ------------- ---------- ----------- ------------- 1-----1-----invoice----400---------02-----1-----payment----250---------03-----1-----invoice----100---------504-----1-----payment----150---------05-----1-----payment----50---------06-----2-----invoice----100---------307-----2-----payment----20---------08-----2-----payment----50---------0how can i do this?(5 row(s) affected)*/ |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2010-10-16 : 11:28:07
|
[code]from @sales s cross apply ( select amount = sum(amount) from @sales x where doc_type = 'payment'and x.cust_id = s.cust_id ) p cross apply ( select inv_amt = sum(amount) from @sales x where x.id < s.id and doc_type = 'invoice'and x.cust_id = s.cust_id ) i[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
sn34
Starting Member
36 Posts |
Posted - 2010-10-16 : 11:56:57
|
Thank you so much and in my database there are data like that:1-----1-----invoice----400---------02-----1-----invoice----( - 400)---------03-----1-----payment---(-250)---------04-----1-----payment----250---------0I whant to filter it where invoice' amount is possitive and where payment's amount is negative and after that calculate remaining, like that:1-----1-----invoice----400---------03-----1-----payment---(-250)---------0can you help me? |
 |
|
|
|
|
|
|