Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old September 4th, 2008, 12:17 PM
Newbie
 
Join Date: Aug 2008
Posts: 12
Default Sum() is making problem

i have a table called bill, in which there r three fields named bill,paid,exempted.
when i when i want to deduct the due of each customer the following query does not work:

Code:
select customer.cc as ccc,
 customer.name as cname,sum(bill.bill) as bill,
 sum(bill.paid) as paid,
 sum(bill)-(sum(paid)+sum(exempted)) as due
 from customer,bill where customer.cc=bill.cc  group by customer.cc
it does not return any result in the 'due' column(just blank) when sum(exempted) is 0.

Whats the problem? Can anyone say?

How to get rid from it?
Reply
  #2  
Old September 4th, 2008, 12:40 PM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 836
Default

Quote:
select customer.cc as ccc,
customer.name as cname,sum(bill.bill) as bill,
sum(bill.paid) as paid,
sum(bill)-(sum(paid)+sum(exempted)) as due
from customer,bill where customer.cc=bill.cc group by customer.cc
I think it is because you have defined bill and paid as alias and this is what you are trying to sum
Try
Code:
select customer.cc as ccc,
 customer.name as cname,sum(bill.bill) as bill,
 sum(bill.paid) as paid,
 bill-paid+sum(exempted) as due
 from customer,bill where customer.cc=bill.cc  group by customer.cc
Reply
  #3  
Old September 4th, 2008, 12:50 PM
Newbie
 
Join Date: Aug 2008
Posts: 12
Default

Quote:
Originally Posted by code green
I think it is because you have defined bill and paid as alias and this is what you are trying to sum
Try
Code:
select customer.cc as ccc,
 customer.name as cname,sum(bill.bill) as bill,
 sum(bill.paid) as paid,
 bill-paid+sum(exempted) as due
 from customer,bill where customer.cc=bill.cc  group by customer.cc
Dear code green,

It does not work.
Reply
  #4  
Old September 4th, 2008, 01:35 PM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 836
Default

Sorry, I was talking rubbish.
Your syntax looks correct.
It might be the bracket arrangement
The brackets are not required mathematically
Code:
sum(bill)-sum(paid)+sum(exempted)
But I can't see that causing a problem.
Maybe the whole equation needs bracketting
Code:
(sum(bill)-sum(paid)+sum(exempted)) AS due
Reply
  #5  
Old September 4th, 2008, 04:23 PM
Newbie
 
Join Date: Aug 2008
Posts: 12
Default

Quote:
Originally Posted by code green
Sorry, I was talking rubbish.
Your syntax looks correct.
It might be the bracket arrangement
The brackets are not required mathematically
Code:
sum(bill)-sum(paid)+sum(exempted)
But I can't see that causing a problem.
Maybe the whole equation needs bracketting
Code:
(sum(bill)-sum(paid)+sum(exempted)) AS due
Dear code green,

it does not work also.
Reply
  #6  
Old September 5th, 2008, 11:20 AM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 836
Default

It is really frustrating when posters use the term 'does not work' as this is very unhelpful.
What results are you getting?
What results do you expect?
What data is present in the relevant tables?

The syntax looks correct to me unless I am missing something.
I am beginning to suspect the data
Reply
  #7  
Old September 5th, 2008, 01:31 PM
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 304
Default

Please post the output from "show create table bill" and "show create table customer", and maybe post some example data that is giving you problems.

This would let someone like myself create the tables quickly and try the query to see what the problem might be.
Reply
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles