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 2008 Forums
 Transact-SQL (2008)
 Get total balance..pls help!

Author  Topic 

blocker
Yak Posting Veteran

89 Posts

Posted - 2011-10-21 : 23:28:39
Good day!
I just want a little help for a query regarding to get the total balance of all the customers.
I want a result just like this:

Juan Dela Cruz - 5,000.00
Pablo Garcia - 10,200.55

This result should be name asc. In tbl_customers, the fields are( [Last Name], [First Name],[Middle Name,],customer_id).

The customers table is in the tbl_customers, and the totalcredit & totalpayment field is in the tbl_transaction_summary where all customers transactions in this table is recorded with the customer_id..

The query is to get the totalbalance where the formula is totalbalance=(totalcredit - totalpayment) in the tbl_transaction_summary order by tbl_customers name.

I just can't figure out how to plot this using one query with inner join, or whatever best query to get this..

Thank you fr helping..Regards..

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-22 : 00:31:48
[code]
select c.[Last Name], c.[First Name],
balance = sum(t.totalcredit - t.totalpayment)
from tbl_customers c
inner join tbl_transaction_summary t on c.customer_id = t.customer_id
group by c.[Last Name], c.[First Name]
[/code]


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

Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2011-10-22 : 02:13:24
Please note that totalcredit is base on field salestype='credit',totalpayment is also base on salestype='pay'.

here is a modified version of the query bu I get an error.

select c.[Last Name], c.[First Name],
currentbalance=(select SUM(t.totalsales) where t.salestype='credit')-(select SUM(t.totalsales) where t.salestype='pay')
from tbl_costumers c
inner join tbl_pos_sales_summary t on c.[ID No.] = t.costumerid
group by c.[Last Name], c.[First Name]

But I get this error:

Msg 8120, Level 16, State 1, Line 2
Column 'tbl_pos_sales_summary.salestype' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.


I cant figure out whats wrong. It seems that the formula is correct.

Thank you!

quote:
Originally posted by khtan


select c.[Last Name], c.[First Name],
balance = sum(t.totalcredit - t.totalpayment)
from tbl_customers c
inner join tbl_transaction_summary t on c.customer_id = t.customer_id
group by c.[Last Name], c.[First Name]



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



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-22 : 02:33:07
[code]
select c.[Last Name],
c.[First Name],
currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)
from tbl_costumers c
inner join tbl_pos_sales_summary t on c.[ID No.] = t.costumerid
group by c.[Last Name],
c.[First Name]
[/code]


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

Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2011-10-22 : 03:36:55
Thank you sir..This works. I've added 'order by' statement on the bottom to sort the result by Last Name.
But how to display result or the currentbalance only those values that are greater than zero?



select c.[Last Name],
c.[First Name],
currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)
from tbl_costumers c
inner join tbl_pos_sales_summary t on c.[ID No.] = t.costumerid


group by c.[Last Name],
c.[First Name]


order by c.[Last Name],
c.[First Name]


Thank you!

quote:
Originally posted by khtan


select c.[Last Name],
c.[First Name],
currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)
from tbl_costumers c
inner join tbl_pos_sales_summary t on c.[ID No.] = t.costumerid
group by c.[Last Name],
c.[First Name]



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



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-22 : 04:31:47
[code]
select c.[Last Name],
c.[First Name],
currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)
from tbl_costumers c
inner join tbl_pos_sales_summary t on c.[ID No.] = t.costumerid


group by c.[Last Name],
c.[First Name]
having sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)>0


order by c.[Last Name],
c.[First Name]
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

blocker
Yak Posting Veteran

89 Posts

Posted - 2011-10-22 : 05:11:44
Thank you!

Can you guys explain this section?..Its running but i need to understand how this works..

currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)


Thank you!

quote:
Originally posted by visakh16


select c.[Last Name],
c.[First Name],
currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)
from tbl_costumers c
inner join tbl_pos_sales_summary t on c.[ID No.] = t.costumerid


group by c.[Last Name],
c.[First Name]
having sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)>0


order by c.[Last Name],
c.[First Name]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-22 : 05:18:50
quote:
Originally posted by blocker

Thank you!

Can you guys explain this section?..Its running but i need to understand how this works..

currentbalance = sum(case when t.salestype = 'credit' then t.totalsales
when t.salestype = 'pay' then -t.totalsales
else 0
end)



sum up the value of column totalsales.
If salestype is credit then just add up the value of column totalsales. If salestype is pay then reverse the sign of the totalsales and add it up.


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

Go to Top of Page
   

- Advertisement -