Solved

How to use complicated nested case statements?

Posted on 2012-12-20
7
465 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 200 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 200 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 200 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 200 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 100 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

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…
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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