June 2, 2011 at 8:59 am
sql server 2008 deriving another column based on other calculated column in SQL Query:
select emp_id, emp_salary_bonus = case some condition,
derived_column = case when emp_salary_bonus <1000 then 1 else 0 end
into #tmp
from emp_table
How can I reference emp_salary_bonus column inside derive_column in the above query?
Please help. Thanks.
June 2, 2011 at 12:28 pm
applebutton (6/2/2011)
sql server 2008 deriving another column based on other calculated column in SQL Query:select emp_id, emp_salary_bonus = case some condition,
derived_column = case when emp_salary_bonus <1000 then 1 else 0 end
into #tmp
from emp_table
How can I reference emp_salary_bonus column inside derive_column in the above query?
Please help. Thanks.
You have a couple of options. You could just leave derived_column null in your temp table and then update it.
update #tmp set derived_column = case when emp_salary_bonus > 1000 then 1 else 0 end
or you will have to create the case statement again
select emp_id, emp_salary_bonus = case some condition,
derived_column = case when [same case as you used in emp_salary_bonus] < 1000 then 1 else 0 end
into #tmp
from emp_table
_______________________________________________________________
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply