Solved

Help with SUM and IF with MySQL

Posted on 2012-03-24
2
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 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