SQL Pivot with just two Columns

I’m trying to do a PIVOT on a table with two rows like this:

Category       |   Sector
Bulbs          |   DIY
Bulbs          |   Home
Picnic blanket |   DIY
Picnic blanket |   Home
Picnic blanket |   Interior

Each category can have 1 or more sectors.

I’m trying to get a table like this:

Category       | Sector 1  | Sector 2  | Sector 3
Bulbs          | DIY       | Home      | NULL
Picnic blanket | DIY       | Home      | Interior

The query looks like this:

    dbo.fn_DbContent_GetTranslation(pt.Name_DbContentId, 2) 'Category'
    , dbo.fn_DbContent_GetTranslation(s.Name_DbContentId, 2) 'Sector'
FROM dbo.ProductType pt
JOIN dbo.ProductTypeMandator ptm ON ptm.ProductTypeId = pt.Id
JOIN dbo.ProductTypeMandator2PortalSector ptmps ON ptmps.ProductTypeMandatorId = ptm.Id
JOIN dbo.PortalSector ps ON ps.Id = ptmps.PortalSectorId
JOIN dbo.Sector s ON s.Id = ps.SectorId
    ptmps.PortalSectorId IN (21, 18, 19)

I already did a PIVOT but with a table containing three columns where one contained the values for the headers in the pivot table. In this case the values for the header are missing so I don’t know how to do it then.

Thanks for helping

Source: sql

Leave a Reply