Sql simple query

Posted by Josemalive on Stack Overflow See other posts from Stack Overflow or by Josemalive
Published on 2010-03-22T15:06:45Z Indexed on 2010/03/22 15:11 UTC
Read the original article Hit count: 439

Filed under:
|
|

Hello,

I have the following table Persons_Companies that shows a relation between persons and companies knowns by these persons:

PersonID  |  CompanyID

   1             1
   2             1
   2             2
   3             2
   4             2

Imaging that company 1="Google" and company 2 is ="Microsoft", i would like to know the query to have the following result:

PersonID  |  Microsoft    |    Google

   1             0                1
   2             1                1
   3             1                0
   4             1                0

Until this moment i have something similar:

select PersonID,
case when CompanyID=1 then 1 else 0
end as Google,
case when EmpresaID=2 then 1 else 0
end as Microsoft
from Persons_Companies

My problem is with the persons that knows both companies, i cant imagine how could this query be.

Could you give me a hand? Thanks in advance. Best Regards. Josema.

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql