Solved

Writing a Case When statement with two conditions

Posted on 2014-02-28
4
297 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
4 Comments
 
LVL 65

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 41

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Article by: Leon
Software Metering within our group of companies has always been an afterthought until auditing of software and licensing became a pain point. Orchestrator and SCCM metering gave us the answer and it was an exciting process.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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

12 Experts available now in Live!

Get 1:1 Help Now