Solved

Add IF THEN clause in SELECT statement

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

20 Experts available now in Live!

Get 1:1 Help Now