Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Creating a CASE login within a CASE logic


Creating a CASE login within a CASE logic

Author
Message
SteveEClarke
SteveEClarke
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 457
I have the following code which seems to accumulate the complete file - rather than the upper record set -

When OperationPhase = 2 and ResCat = 'MC' - I want to sum it.



SUM( CASE OperationPhase WHEN 2 THEN
(CASE ResCat when 'MC' then ResAllocTime else 0 end ) else 0 end ) as TotalSetUpMC,
SUM( CASE OperationPhase WHEN 3 THEN
(CASE ResCat when 'MC' then ResAllocTime else 0 end ) else 0 end ) as TotalRunMC,
SUM( CASE OperationPhase WHEN 2 THEN
(CASE ResCat when 'PERS' then ResAllocTime else 0 end ) else 0 end ) as TotalSetUpPERS,
SUM( CASE OperationPhase WHEN 3 THEN
(CASE ResCat when 'PERS' then ResAllocTime else 0 end ) else 0 end ) as TotalRunPERS,

________________________________________________________________________________________________
Regards
Steve
SQL 2008 DBA/DBD - MCTS/MCITP

Please don't trust me, test the solutions I give you before using them.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16545 Visits: 16999
SteveEClarke (9/25/2013)
I have the following code which seems to accumulate the complete file - rather than the upper record set -

When OperationPhase = 2 and ResCat = 'MC' - I want to sum it.



SUM( CASE OperationPhase WHEN 2 THEN
(CASE ResCat when 'MC' then ResAllocTime else 0 end ) else 0 end ) as TotalSetUpMC,
SUM( CASE OperationPhase WHEN 3 THEN
(CASE ResCat when 'MC' then ResAllocTime else 0 end ) else 0 end ) as TotalRunMC,
SUM( CASE OperationPhase WHEN 2 THEN
(CASE ResCat when 'PERS' then ResAllocTime else 0 end ) else 0 end ) as TotalSetUpPERS,
SUM( CASE OperationPhase WHEN 3 THEN
(CASE ResCat when 'PERS' then ResAllocTime else 0 end ) else 0 end ) as TotalRunPERS,



Need a little more detail here. What does upper record set mean? What exactly are you trying to do?

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SteveEClarke
SteveEClarke
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 457
Sorry -

What I am trying to achieve is a CASE within a CASE I suppose

Two conditional checks ...

So ...

SUM ( CASE field_SEX WHEN "MALE" THEN
CASE field_PARENTS_ALIVE WHEN "YES" Then 1 ELSE 0 END) ELSE 0 END ) as MaleParents

SUM ( CASE field_SEX WHEN "FEMALE" THEN
CASE field_PARENTS_ALIVE WHEN "YES" Then 1 ELSE 0 END) ELSE 0 END ) as FeMaleParents

Does that make more sense ?

________________________________________________________________________________________________
Regards
Steve
SQL 2008 DBA/DBD - MCTS/MCITP

Please don't trust me, test the solutions I give you before using them.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16545 Visits: 16999
SteveEClarke (9/25/2013)
Sorry -

What I am trying to achieve is a CASE within a CASE I suppose

Two conditional checks ...

So ...

SUM ( CASE field_SEX WHEN "MALE" THEN
CASE field_PARENTS_ALIVE WHEN "YES" Then 1 ELSE 0 END) ELSE 0 END ) as MaleParents

SUM ( CASE field_SEX WHEN "FEMALE" THEN
CASE field_PARENTS_ALIVE WHEN "YES" Then 1 ELSE 0 END) ELSE 0 END ) as FeMaleParents

Does that make more sense ?


For two conditions just use AND as your check.


SUM(CASE WHEN field_SEX = 'MALE' AND field_PARENTS_ALIVE = 'YES' Then 1 ELSE 0 END) as MaleParents

SUM(CASE WHEN field_SEX = 'FEMALE' AND field_PARENTS_ALIVE = 'YES' Then 1 ELSE 0 END) as FeMaleParents



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
SteveEClarke
SteveEClarke
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 457
Sorry - that seems to be working as expected -

Thanks -

I think when I tried that I had "CASE fieldname WHEN" rather than "CASE WHEN fieldname"

Regards

________________________________________________________________________________________________
Regards
Steve
SQL 2008 DBA/DBD - MCTS/MCITP

Please don't trust me, test the solutions I give you before using them.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search