Solved

Add IF THEN clause in SELECT statement

Posted on 2008-06-17
2
818 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
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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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