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
 General SQL Server Forums
 New to SQL Server Programming
 how to group debit credit using one column?

Author  Topic 

dohamsg
Starting Member

22 Posts

Posted - 2009-08-01 : 18:57:58
Hi, I use SQL Server 2008

this is my transaction table:
------------------------------
TransactionID, TransactionDate, Amount, FromAccountID, ToAccountID;

When I post a transaction : Amount is subtracted from FromAccountID and added to ToAccountID.

This way, given an AccountID, it could be listed in FromAccountID in some Transactions, and in ToAccountID in other Transactions.

The problem I'm facing is how to get the balance of Accounts in a given day?

Or how to group transactions of a given day based on AccountIDs using Addition of Amount column when an AccountID is in ToAccountID column and subtract the Amount column if the AccountID is in the FromAccountID column, for each AccountID?

Example:

TransactionID TransactionDate Amount FromAccountID ToAccountID
------------------------------------------------------------------
1 ____________ today __________ 100.00 _______ 1000 _______ 1001
2 ____________ today ___________ 40.00 _______ 1002 _______ 1000

After Calc, I want to have the listing of accounts used in a given day, with the totals, for example the AccountID = 1000 should total to -100.00 + 40.00 = -60.00
AccountID = 1001 should total to 100.00
AccountID = 1002 should total to -40
Thanks.

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2009-08-01 : 21:10:23
I think the best way would be to have 2 views (ins & outs) joined by account ID. Something like this (disclaimer: I have no idea if it works or compiles)

select
accountID ins.accountID, coalesce(allIns,0)-coalesce(AllOuts,0)
from
(select toAccountID accountID, sum(ammount) allIns from T where transactiondate=@date group by toAccountID) ins
full outer join
(select fromAccountID accountID, sum(ammount) allOuts from T where transactiondate=@date group by toAccountID) outs
on (ins.accountID=outs.accountID)
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-08-02 : 00:57:54


DECLARE @sample TABLE
(
TransactionID int,
TransactionDate datetime,
Amount decimal(10,2),
FromAccountID int,
ToAccountID int
)
INSERT INTO @sample
SELECT 1, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0), 100.00, 1000, 1001 UNION ALL
SELECT 2, DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0), 40.00, 1002, 1000

SELECT AccountID, Amount = SUM(Amount)
FROM
(
SELECT AccountID = FromAccountID,
Amount = -Amount
FROM @sample s
WHERE s.TransactionDate = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

UNION ALL

SELECT AccountID = ToAccountID,
Amount = Amount
FROM @sample s
WHERE s.TransactionDate = DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)
) a
GROUP BY AccountID

/*
AccountID Amount
----------- ------
1000 -60.00
1001 100.00
1002 -40.00

(3 row(s) affected)
*/




KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -