Question : sql query help

I need help writing a sql query.  This query works but there are 6 different divisions, however division 17 employees need to use a another division's address.  the costcenter column represents which division that needs to be used instead of the 17 divison.  How can I bascially make a case statement and then use that value other places in the same query.  The NewDiv says it cant be found.


,case
when hr.costcenter = '17-11F' then '011'
when hr.costcenter = '17-12F' then '012'
when hr.costcenter = '17-13F' then '013'
when hr.costcenter = '17-14F' then '014'
when hr.costcenter = '17-16F' then '016'
else 99 end as NewDiv
 
From humres as hr  
--
left join cicmpy ON NewDiv = cicmpy.administration and cicmpy.cmp_type = 'D'
--
Order by hr.res_id


 

Then change out this line as
left join cicmpy ON hr.comp = cicmpy.administration and cicmpy.cmp_type = 'D'
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
Select  
hr.res_id  
,hr.fullname   
,hr.usr_id  
,jt.descr50  
,hr.comp  
,hr.job_title  
,hr.loc  
, cicmpy.cmp_fadd1 as address 
, cicmpy.cmp_tel as phone 
, cicmpy.cmp_fax as fax 
, cicmpy.cmp_fcity COLLATE DATABASE_DEFAULT+ ', ' + cicmpy.statecode COLLATE DATABASE_DEFAULT+ ' ' + cicmpy.cmp_fpc COLLATE DATABASE_DEFAULT as citystate 
,hr.ldatindienst  
,hr.emp_stat  
,hr.costcenter  
,a.itemcode  
,i.userYesNo_01  
,a.enddate 

From humres as hr  
left join (absences a inner join items i on i.itemcode = a.itemcode and i.userYesNo_01 = 1) on hr.res_id = a.empid and a.type = 86  
left join hrjbtl as jt  on hr.job_title = jt.job_title  
-- 
left join cicmpy ON hr.comp= cicmpy.administration and cicmpy.cmp_type = 'D' 
where usr_id = 'T0798'
-- 
Order by hr.res_id

Answer : sql query help

Mine was only example of how to achieve your results.

Now attached is your original query modified to accommodate my earlier suggestion

in the first SELECT list you can now use hr.NewDiv
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
Select  
	hr.res_id  
	,hr.fullname   
	,hr.usr_id  
	,jt.descr50  
	,hr.comp  
	,hr.job_title  
	,hr.loc  
	, cicmpy.cmp_fadd1 as address 
	, cicmpy.cmp_tel as phone 
	, cicmpy.cmp_fax as fax 
	, cicmpy.cmp_fcity COLLATE DATABASE_DEFAULT+ ', ' + cicmpy.statecode COLLATE DATABASE_DEFAULT+ ' ' + cicmpy.cmp_fpc COLLATE DATABASE_DEFAULT as citystate 
	,hr.ldatindienst  
	,hr.emp_stat  
	,hr.NewDiv --hr.costcenter  
	,a.itemcode  
	,i.userYesNo_01  
	,a.enddate 
From 
(
	select *
		,case when costcenter = '17-11F' then '011'
                when costcenter = '17-12F' then '012'
                when costcenter = '17-13F' then '013'
                when costcenter = '17-14F' then '014'
                when costcenter = '17-16F' then '016'
                else 99 end as NewDiv 
	from humres
) as hr  
left join (absences a inner join items i on i.itemcode = a.itemcode and i.userYesNo_01 = 1) on hr.res_id = a.empid and a.type = 86  
left join hrjbtl as jt  on hr.job_title = jt.job_title  
-- 
left join cicmpy ON hr.comp= cicmpy.administration and cicmpy.cmp_type = 'D' 
where usr_id = 'T0798'
-- 
Order by hr.res_id
Random Solutions  
 
programming4us programming4us