?
Solved

How to use complicated nested case statements?

Posted on 2012-12-20
7
Medium Priority
?
502 Views
Last Modified: 2012-12-31
I have a SQL statement to calculate some stats data for analytical report purpose, it's working good:

select
SUM(CASE WHEN (createDate = CURRENT_DATE AND exitDate IS NULL) THEN 1 ELSE 0 END)
...
from sample table
group by ...

Now I want to add more complicated logic to the script (adding nested "AND" and "OR" logic), something like:

select
SUM(CASE WHEN (createDate = CURRENT_DATE) AND (exitDate IS NULL OR (exitDate > CURRENT_DATE)) THEN 1 ELSE 0 END) AS survey_Tasks_Started_And_Not_Completed,
...
from sample table
group by...

The purpose is, when createDate = CURRENT_DATE, I want to include records of both (exitDate field is null) or (exitDate is later than today)

But seems it's not working if I add too many parenthesis and nested statements... Any solution for this kind of issue when adding complicated nested case statements in MySQL?

Or is there any recommendation for better solution for such common scenarios since too many nested level cause issue too?

Thanks!
0
Comment
Question by:heyday2004
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 800 total points
ID: 38711758
No reason that I know of for that not to work, of you post the actual query, and the results that suggest its not working and maybe someone can help further.
0
 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 800 total points
ID: 38711892
When my stuff gets complex I use CTE to clean it up.

With stuff as (
Select
(case when blah and blah then 1) as startedandnotcompleted from sampletable
)
Select count(*) from stuff where startedandnotcompleted = 1
0
 

Author Comment

by:heyday2004
ID: 38711981
Thanks, any further suggestion on the best practice to deal with such complicated nested scenario? I think it's very error-prone.  Thanks.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 800 total points
ID: 38711993
There's nothing wrong with using complex/nested case statements, its a perfectly good solution to the right problem. All SQL is error prone - its very easy to get a query that works without giving you the data you want. Please post you query and the incorrect result as its not really possible to help you further in a general sense.
0
 
LVL 39

Assisted Solution

by:Aaron Tomosky
Aaron Tomosky earned 800 total points
ID: 38712014
Agreed, nothing wrong with complex nested case, I think the issue becomes when you add the group by and such. The trick to complex SQL is to do it one step at a time. With sub querries or CTE, it lets you do one thing at a time, see the results, an build the next step off it.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 400 total points
ID: 38713919
No need for nesting conditions, just stack them -
SUM(CASE WHEN createDate = CURRENT_DATE AND exitDate IS NULL THEN 1
                 WHEN createDate = CURRENT_DATE AND exitDate > CURRENT_DATE THEN 1
                 ELSE 0
         END) AS survey_Tasks_Started_And_Not_Completed,
...
Note - when using case like this, it is always best to list the condition most likely to occur first, second most second, etc.
0
 

Author Closing Comment

by:heyday2004
ID: 38733705
Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

831 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