Solved

Access Iif  to MySQL Case?

Posted on 2008-10-13
4
824 Views
Last Modified: 2012-05-05
I have a simple query that totals weekday vs. weekend sales in MS Access that I need to translate to MySQL.

In Access it is:

SELECT
daily_store_sales.account_code,
Sum(IIf(DatePart("w",[store_report_date]) IN (2,3,4,5,6),qty_daily_sales,0)) AS WDTotal,
Sum(IIf(DatePart("w",[store_report_date]) In (1,7),qty_daily_sales,0)) AS WETotal,
Sum(qty_daily_sales)
FROM
daily_store_sales
GROUP BY
daily_store_sales.account_code

My MySQL translation (of many variations) is not working:

SELECT
daily_store_sales.account_code,
Sum(CASE WHEN DAYOFWEEK([store_report_date]) IN (2,3,4,5,6) THEN SELECT qty_daily_sales ELSE SELECT 0)) AS WDTotal
FROM
daily_store_sales
GROUP BY
daily_store_sales.account_code

Can CASE be used in SELECTs like this?  And if not, what is a better way to handle if/then logic in MySQL queries?

Many Thanks.  
0
Comment
Question by:bishopkd
  • 2
  • 2
4 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22706476
yes, but no the SELECT inside the CASE..
Sum(CASE WHEN DAYOFWEEK([store_report_date]) IN (2,3,4,5,6) THEN qty_daily_sales ELSE 0 END)) AS WDTotal

Open in new window

0
 

Author Comment

by:bishopkd
ID: 22710669
Thanks, but this is still giving me a syntax error for this line.  Any other thoughts?
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 22710775
sorry. the [] in ms access are `` in mysql:
Sum(CASE WHEN DAYOFWEEK(`store_report_date`) IN (2,3,4,5,6) THEN qty_daily_sales ELSE 0 END)) AS WDTotal

Open in new window

0
 

Author Comment

by:bishopkd
ID: 22710798
You are a prince among men.  Thank you!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

18 Experts available now in Live!

Get 1:1 Help Now