Solved

Help with SUM and IF with MySQL

Posted on 2012-03-24
2
302 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
Comment Utility
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
Comment Utility
good job, you got to it quick!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

744 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