Solved

MySQL sum() case/if

Posted on 2010-08-26
2
864 Views
Last Modified: 2012-08-13
Hi All,

This is a related question.

I need to calculate the cost to get a result similar to the following;

status     chargeable    nonchargeable     cost
loan1                  100                      300     400
loan2                  500                      300     800
loan3                  100                      300     400

I've tried to use the code to calculate the cost of each but cant get the correct syntax, instead I just calculate the number of entries where the status is chargeable (that'll be the 1 else 0 bit) I have tried modifying the syntax to use a.cost but it just keeps giving an error.

Current code is below.
select b.booking_type as status,
      sum(case when a.chargeable = 'Yes' then 1 else 0 end) chargeable,
      sum(case when a.chargeable = 'No' then 1 else 0 end) nonchargeable,
      sum(a.cost) as cost
from BOOKING b, ADMIN a
where b.booking_id = a.booking_id
and a.approved = "Approved"
and a.delivery_date between '2010-07-01' and '2010-07-31'
group by booking_type

Open in new window

0
Comment
Question by:jools
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33534192
select b.booking_type as status,
      sum(case when a.chargeable = 'Yes' then a.cost else 0 end) chargeable,
      sum(case when a.chargeable = 'No' then a.cost else 0 end) nonchargeable,
      sum(a.cost) as cost
from BOOKING b, ADMIN a
where b.booking_id = a.booking_id
and a.approved = "Approved"
and a.delivery_date between '2010-07-01' and '2010-07-31'
group by booking_type
0
 
LVL 19

Author Closing Comment

by:jools
ID: 33534272
Cheers AngelIII,

I was trying to use `then sum(a.cost)...` and wasnt getting anywhere...

I can now finish for the day happy :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword This article was written many years ago, in the days when PHP supported the MySQL extension (http://php.net/manual/en/function.mysql-connect.php).  Today (http://php.net/manual/en/migration70.removed-exts-sapis.php) you would not use MySQL…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question