Click here to Skip to main content
15,919,422 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

i have situation like bellow

table 1 have id and table2 have id, company id

i wrote case statement for insert a flag in a new table by joining the table1 and table2

VB
select table1.id,
       flag= case when companyid=1 then 'c'
                  when companyid=<>1 then 'd'
             end
from table1 join table2 table1.id=table2.id


XML
But here my problem i want a flag with both id's are matching and it is in companyid=1 and companyid<>1 AS 'both'

Thanks
Ramesh.m
Posted

SQL
select table1.id,
     case when companyid=1 then 'c' Else 'd' end as flag
from table1 join table2 table1.id=table2.id

Happy Coding!
:)
 
Share this answer
 
Comments
ramesh4128 6-May-13 5:11am    
But here my problem i want a flag with both id's are matching and it is in companyid=1 and companyid<>1 AS 'both'

Thanks
Ramesh.m
Aarti Meswania 6-May-13 5:20am    
it's unclear
if companyid=1 and companyid<>1 then there is no meaning to write this condition because it will always goes to false part...this cond. will never true
ramesh4128 6-May-13 5:26am    
i have a customer a/c that will map with another table and pull the data but one column should be update like if it match with condition 1 the display value 1 , if it match with condition 2 then display with value2. if it satisfy with condition 1 and condition 2 then values 3
Aarti Meswania 6-May-13 6:14am    
then you should use inner case statements
e.g.
select table1.id,
case when companyid=1 and CompanyId<>1 then 'c'
Else
case when companyid=1 then 'd'
else
'e'
end
end as flag
from table1 join table2 table1.id=table2.id
1. solution
SQL
SELECT t1.id, [flag] = CASE
    WHEN companyid=1 THEN 'c'
    WHEN companyid=2 THEN 'd'
    WHEN companyid=3 THEN 'e'
    WHEN companyid=4 THEN 'f'
    WHEN companyid=5 THEN 'g'
    ELSE 'h'END
FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id=t2.id


More about: CASE (T-SQL)[^]

2. solution:
SQL
SELECT T.* 
FROM (
    SELECT t1.id, 'c' AS [flag]
    FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id=t2.id
    WHERE companyid = 1
    UNION ALL
    SELECT t1.id, 'd' AS [flag]
    FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id=t2.id
    WHERE companyid <> 1 AND IntField=5
    UNION ALL
    SELECT t1.id, 'c' AS [flag]
    FROM table1 AS t1 INNER JOIN table2 AS t2 ON t1.id=t2.id
    WHERE companyid = 1 AND IntField=9 AND TextField='CP'
) AS T


More about: UNION (T-SQL)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900