Solved

How to use complicated nested case statements?

Posted on 2012-12-20
7
468 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Webinar: Choosing a MySQL HA Solution

Join Percona’s Principal Technical Services Engineer, Marcos Albe as he presents Choosing a MySQL High Availability Solution on Thursday, June 29, 2017 at 10:00 am PDT / 2:00 pm EDT (UTC-7).

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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