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

x
?
Solved

Add IF THEN clause in SELECT statement

Posted on 2008-06-17
2
Medium Priority
?
824 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 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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

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…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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
Course of the Month11 days, 4 hours left to enroll

885 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