Solved

Help with SUM and IF with MySQL

Posted on 2012-03-24
2
303 Views
Last Modified: 2012-03-24
I am trying to query the sum of products by category.  I have prices for New and Used.  I am trying to just show a report of overall sales by category.  The new price and used prices are in two different columns.

Here is a general idea of what i am trying to do.  This is Mysql 5.  My example below is just me talking it out in a general matter to get the idea across.

select sum(if condition is new price new, else priceUsed) as totalValue
from products
group by category

I am not sure how to do the sum with the if statement, if that is even possible.
0
Comment
Question by:theideabulb
2 Comments
 
LVL 2

Accepted Solution

by:
Kelden earned 500 total points
ID: 37761299
Try:

select sum( case when new then newprice else priceused end ) as totalValue, category
from products
group by category
0
 

Author Closing Comment

by:theideabulb
ID: 37761328
good job, you got to it quick!
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Foreword This is an old article.  Instead of using the MySQL extension that was used in the original code examples, please choose one of the currently supported database extensions instead.  More information is available here: MySQLi / PDO (http://…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 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