Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 select case when help

Author  Topic 

shorsh
Starting Member

1 Post

Posted - 2014-10-23 : 14:35:09
Hi - I have two tables (tbl1 and tbl2). based on m_code value in tbl1, I want to display P_CODE and E_CODE fields in tbl2 certain way. Below is how I want to write my query, I appreciate if you please help me with correcting the query!


select
case
when e.m_code = 'S' then s.P_CODE = 1 and s.E_CODE = 1
when e.m_code = 'H' then s.P_CODE = 2 and s.E_CODE =1
when e.m_code = 'E' then s.P_CODE = 3 and s.E_CODE =1
when e.m_code = 'P' then s.P_CODE = 3 and s.E_CODE = 1
when e.m_code ='R' then s.P_CODE = 3 and s.E_CODE =1
end

from
tbl1 e
join tbl2 s on e.field = s.field
where ese.m_code in (
'S',
'H',
'E',
'P',
'R')

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-10-23 : 14:38:11
[code]
select
case
when e.m_code = 'S' and s.E_CODE = 1 then 1
when e.m_code = 'H' and s.E_CODE =1 then 2
when e.m_code = 'E' and s.E_CODE =1 then 3
when e.m_code = 'P' and s.E_CODE = 1 then 3
when e.m_code ='R' and s.E_CODE = 1 then 3
end as P_CODE

[/code]
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-10-25 : 15:41:25
[code]
select
P_CODE = case
when e.m_code = 'S' then 1
when e.m_code = 'H' then 2
when e.m_code IN ('E','P','R') then 3
end,
s.E_CODE =1

from
tbl1 e
join tbl2 s on e.field = s.field
where ese.m_code in (
'S',
'H',
'E',
'P',
'R')
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -