Solved

Add IF THEN clause in SELECT statement

Posted on 2008-06-17
2
812 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 142

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
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 …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

15 Experts available now in Live!

Get 1:1 Help Now