Create table ProductColorTable (ProductId int, Color varchar(20) )
Insert into ProductColorTable
values(1,'Red'),(1,'Blue'),(1,'Green')
,(2,'Red')
,(3,'Yellow'),(3,'Green')
,(4,'Red'),(4,'Blue')
;with cteColor as (
Select ProductId, Color,
row_number() over(partition by productid order by color) rn
from ProductColorTable)
select ProductId,
Max(Case when rn=1 then Color else null end) Color1,
Max(Case when rn=2 then Color else null end) Color2,
Max(Case when rn=3 then Color else null end) Color3,
Max(Case when rn=4 then Color else null end) Color4,
Max(Case when rn=5 then Color else null end) Color5
from cteColor
WHERE ProductId in (Select ProductId from cteColor where Color='Blue' )
Group by ProductId
drop table ProductColorTable