Solved

MySQL sum() case/if

Posted on 2010-08-26
2
847 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 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now