Solved

Writing a Case When statement with two conditions

Posted on 2014-02-28
4
328 Views
Last Modified: 2014-03-03
I am trying to write a Case When statement with two conditions.  In addition it has to be a MAX value since I am grouping everything by a field.

I have tried the code:

Max(Case When InsOrd=1 then When PtBal=Balance then 0 else Balance end) as InsBal1,
Max(Case when InsOrd =2 then when PtBal =Balance then 0 else Balance end) as InsBal2,
max(Case when InsOrd =3 then When PtBal=Balance then 0 else Balance end) as InsBal3,


I get errors that the WHEN is not correct.

The Balance  or Zero value should output to InsBal1,2,3 depending on the InsOrd value.

Thanks

Glen
0
Comment
Question by:GPSPOW
[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
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 167 total points
ID: 39895593
If you have two WHENs, you'll need two THENs (which you have) and two ENDs (which you only have one.  

Try this
Max(Case When InsOrd=1 then When PtBal=Balance then 0 else Balance end else 0 end) as InsBal1,

Also, I wrote an article on SQL Server CASE Solutions, where halfway down it shows an example of a nested CASE statement.
0
 
LVL 11

Assisted Solution

by:David Kroll
David Kroll earned 167 total points
ID: 39895815
Max(
Case
  When InsOrd=1 then
    case
      When PtBal=Balance then 0
      else Balance
    end
   else 0
end) as InsBal1
0
 
LVL 42

Assisted Solution

by:pcelba
pcelba earned 166 total points
ID: 39896281
Interesting... I've post an answer a few hours ago but it seems to be lost in the universe... OK, here it is again.

AND operator is simpler than two nested CASE WHEN structures:

Max(Case When InsOrd=1 AND PtBal=Balance then 0 else Balance end) as InsBal1,
Max(Case when InsOrd =2 AND PtBal =Balance then 0 else Balance end) as InsBal2,
max(Case when InsOrd =3 AND PtBal=Balance then 0 else Balance end) as InsBal3,
0
 

Author Closing Comment

by:GPSPOW
ID: 39901082
Thanks to all.

Everyone of the solutions are viable for my project.

Glen
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

617 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