Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use complicated nested case statements?

Posted on 2012-12-20
7
Medium Priority
?
489 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 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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

618 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