Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Add IF THEN clause in SELECT statement

Posted on 2008-06-17
2
Medium Priority
?
821 Views
Last Modified: 2008-08-20
Using MySQL 5 and Coldfusion8. I have fields for "price" and "price_status" among other fields. Price_status has 2 options, set and notset.  The user will enter a price and select if that price is set or not.  

My select query looks like this...

SELECT SUM(amount) as amount, ROUND(AVG(weight)) as avg_weight,  ROUND(AVG(price),2) as avg_price
FROM deal_master LEFT JOIN (deal_lots LEFT JOIN sale_lots ON deal_lots.lot_id = sale_lots.origin_lot_id) ON deal_lots.deal_master_id = deal_master.deal_master_id
WHERE sale_lots.delivery_taken_date IS NULL

1 of my inventory units includes fields for amount, weight and price.  In this query, I want to include the amount and weight from all of my products but I only want to average the prices that are set.  I can't add a where clause like WHERE price_type = set since that would then exclude all of my weight and amounts.  Is there anyway I could tell it to use weights and amounts on all inventory, but only use prices where price_type = set?  
0
Comment
Question by:MFredin
[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 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21804857
you are looking for the CASE WHEN <condition> THEN <value> ... END  syntax structure:
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html
0
 
LVL 20

Accepted Solution

by:
virmaior earned 2000 total points
ID: 21812933
in order to get this to work, you have to make an additional change in addition to the IF clause.  You can use your current query and only include the data that matter as below (but then you must manually do the AVERAGE).

Or you can rearrange your query into two subqueries one of which includes all of the data and the other of which contains only the data with set prices.  In the latter case, you can use the AVG function but then you are querying the data twice anyway.
SELECT SUM(amount) as amount, 
ROUND(AVG(weight)) as avg_weight,  
ROUND(
        SUM(
              IF(price_status = 'set',price,0)
        ) / COUNT(IF(price_status = 'set',price,1)),2) as avg_price
FROM deal_master LEFT JOIN (deal_lots LEFT JOIN sale_lots ON deal_lots.lot_id = sale_lots.origin_lot_id) ON deal_lots.deal_master_id = deal_master.deal_master_id
WHERE sale_lots.delivery_taken_date IS NULL

Open in new window

0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
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 Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

705 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