Solved

Access Iif  to MySQL Case?

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Pivot Rows To Columns 10 53
MS SQL Server - Looking to filter rows based on column value 3 37
Sql server function help 15 29
Complex SQL statement in VB.NET 3 8
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…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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