Solved

Access Iif  to MySQL Case?

Posted on 2008-10-13
4
823 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video discusses moving either the default database or any database to a new volume.
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…

706 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

21 Experts available now in Live!

Get 1:1 Help Now