Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MySQL sum() case/if

Posted on 2010-08-26
2
Medium Priority
?
881 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
2 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month12 days, 12 hours left to enroll

971 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