How to get data Horizontally from table Compliance Data Based on Part Id and Compliance Type Id?

I work on SQL Server 2014 I need to get data from compliance data table horizontally.

  • based on part id and compliance type Id 1,2,11
  • every part id will have one row per 3 compliance type
  • every row per part will have 3 compliance type id 1,2,11
  • if part not have 3 compliance then it will take Null on empty compliance Type
  • as part id 749120,4620

How to do that please?

Sample data as below

create table #ComplianceData 
(
    PartId int,
    ComplianceTypeID int,
    CompStatus nvarchar(30),
    VersionOrder int,
    ComplianceType nvarchar(30)
)

insert into # ComplianceData (PartId, ComplianceTypeID, CompStatus, VersionOrder, ComplianceType)
values (5090, 1, 'Compliant', 3, 'Rohs'),
       (5090, 1, 'NotCompliant', 40, 'Rohs'),
       (5090, 2, 'Compliant', 25, 'Reach'),
       (5090, 11, 'NotCompliant', 1, 'TSKA'),
       (2306, 1, 'Compliant', 3, 'Rohs'),
       (2306, 2, 'NotCompliant', 25, 'Reach'),
       (2306, 11, 'Compliant', 1, 'TSKA'),
       (4620, 1, 'NotCompliant', 3, 'Rohs'),
       (4620, 2, 'Compliant', 25, 'Reach'),
       (749120, 2, 'Compliant', 25, 'Reach')

Result required as

Display Data Horizontally Based On Part Id

Samantha Potts

Author: Samantha Potts