SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help with SQL IF
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eljapo4
Yak Posting Veteran

United Kingdom
94 Posts

Posted - 05/01/2012 :  07:35:41  Show Profile  Reply with Quote
Hi I've the following code as part of a select statement:

CASE WHEN D1_Factor_ID = '1' THEN
  SUM(EffectiveMinutes * D1_Factor_PC)/100
ELSE
  0
END AS [Personal_Needs],			
IF (Personal_Needs <> 0)
SUM(EffectiveMinutes) + SUM(EffectiveMinutes * D1_Factor_PC)/100 AS 'SAM Recovery + Personal Needs Total',

Can I use the created col from the CASE in my IF? At the minute I'm getting the following errors:
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near 'SUM'.

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 05/01/2012 :  08:25:44  Show Profile  Reply with Quote
You cannot use aliases you define in the select clause within the same select clause (or anywhere else in that select statement , except in the order by clause). So what you may need to do is something like this where I am repeating the logic:
CASE WHEN D1_Factor_ID = '1' THEN
  SUM(EffectiveMinutes * D1_Factor_PC)/100.0
ELSE
  0
END AS [Personal_Needs],

CASE WHEN D1_Factor_ID = '1' AND SUM(EffectiveMinutes * D1_Factor_PC)/100.0 <> 0 THEN
	SUM(EffectiveMinutes) + SUM(EffectiveMinutes * D1_Factor_PC)/100.0
ELSE
	0
END AS 'SAM Recovery + Personal Needs Total',
One other thing to keep in mind is the following: If your Effective minutes and D1_Factor_PC are integers, the resulting division will end up being an integer division, which may not be what you want. Hence my adding a ".0" (see in red) (to force it to have fractional parts) in the above query
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 05/01/2012 :  10:09:57  Show Profile  Reply with Quote
if you dont want to repeat the case then only alternative is

SELECT ...,
CASE WHEN(Personal_Needs <> 0) THEN 
SUM(EffectiveMinutes) + SUM(EffectiveMinutes * D1_Factor_PC)/100 END AS 'SAM Recovery + Personal Needs Total',
...
FROM
(

SELECT CASE WHEN D1_Factor_ID = '1' THEN
  SUM(EffectiveMinutes * D1_Factor_PC)/100
ELSE
  0
END AS [Personal_Needs],			
...

)t
...





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

eljapo4
Yak Posting Veteran

United Kingdom
94 Posts

Posted - 05/02/2012 :  04:44:41  Show Profile  Reply with Quote
thank you both for your replies, they helped me solve me problem.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47173 Posts

Posted - 05/02/2012 :  19:42:27  Show Profile  Reply with Quote
wc

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000