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

x
?
Solved

Help with SUM and IF with MySQL

Posted on 2012-03-24
2
Medium Priority
?
312 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 Month13 days, 1 hour left to enroll

972 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