Reputation: 101
I have a table containing customer ledger entries, where every transaction amount is recorded (including charges and payments). I want to get the balance for each customer in a query probably using the sum function (The sum of the balances gives the balance).
How do i achieve it?
Here is some sample data (I have filtered for only one customer though I have thousands of them).
amount Customer No.
-14,453.00 2709
-12,000.00 2709
-9,200.00 2709
-8,000.00 2709
-8,000.00 2709
-8,000.00 2709
-8,000.00 2709
-7,998.20 2709
-7,997.20 2709
-5,568.00 2709
-4,300.00 2709
-4,001.80 2709
-4,000.20 2709
-4,000.00 2709
-4,000.00 2709
-4,000.00 2709
-4,000.00 2709
-4,000.00 2709
-4,000.00 2709
-4,000.00 2709
-4,000.00 2709
-3,999.80 2709
-3,998.20 2709
-2,953.00 2709
-2,841.80 2709
-2,596.80 2709
-2,558.20 2709
-2,381.20 2709
-2,101.80 2709
-2,000.00 2709
-1,800.00 2709
-1,771.80 2709
-1,737.00 2709
-1,737.00 2709
-1,737.00 2709
-1,736.80 2709
-1,500.00 2709
-1,368.80 2709
-1,181.20 2709
-870 2709
-870 2709
-870 2709
-870 2709
-870 2709
-870 2709
-870 2709
-870 2709
-870 2709
-851.2 2709
-820 2709
-816.8 2709
-800 2709
-800 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-580 2709
-571.8 2709
-533.2 2709
-370 2709
-368.2 2709
-356.2 2709
-200 2709
-200 2709
-176.8 2709
-131.8 2709
-41.8 2709
-28.2 2709
-23.2 2709
-2.8 2709
-2.8 2709
-1.8 2709
-0.2 2709
1.8 2709
1.8 2709
2.8 2709
23.2 2709
131.8 2709
200 2709
200 2709
200 2709
368.2 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
800 2709
820 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
1,160.00 2709
1,500.00 2709
1,737.00 2709
1,737.00 2709
1,737.00 2709
1,737.00 2709
1,737.00 2709
1,737.00 2709
1,800.00 2709
2,000.00 2709
2,101.80 2709
2,953.00 2709
2,953.00 2709
2,953.00 2709
2,953.00 2709
2,953.00 2709
2,953.00 2709
3,375.00 2709
3,375.00 2709
3,998.20 2709
4,000.00 2709
4,000.00 2709
4,000.00 2709
4,000.00 2709
4,000.00 2709
4,000.00 2709
4,000.00 2709
4,300.00 2709
5,568.00 2709
7,868.20 2709
7,976.80 2709
8,000.00 2709
8,000.00 2709
8,000.00 2709
8,000.00 2709
8,000.00 2709
8,000.00 2709
8,000.00 2709
9,200.00 2709
14,450.20 2709
Upvotes: 1
Views: 57
Reputation: 1
use sum
and where
SELECT SUM(amount)
FROM table_name WHERE Customer_No=2709;
Upvotes: 0
Reputation: 2481
Just use SUM
AND GROUP BY
SELECT SUM(amount), Customer_No
from table
group by Customer_No
Upvotes: 5