Solved

How to use complicated nested case statements?

Posted on 2012-12-20
7
428 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 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 38

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 38

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 31

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

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

13 Experts available now in Live!

Get 1:1 Help Now