I work on sql server 2014 i need to pivot data for multi column as supplier percentage and material percentage
my sample data as below :
CREATE TABLE [dbo].[TempMaterial](
[CompanyID] [bigint] NOT NULL,
[Year] [int] NOT NULL,
[Rev_ID] [int] NULL,
[MetarialID] [int] NULL,
[Metarialperc] [float] NULL,
[SupplierPerc] [decimal](18, 2) NULL
) ON [PRIMARY]
GO
---select * from [dbo].[TempMaterial]
insert into [dbo].[TempMaterial](CompanyID,Year,Rev_ID,MetarialID,Metarialperc,SupplierPerc)
select 1039152, 2020,339898,1888574,80.18,79.18
union
select 1039152, 2020,339898,1888575,100,99.00
union
select 1039152, 2020,339898,1888576,98.87,96.87
union
select 1039152, 2020,339898,1888577,93.74,90.74
union
select 1039109, 2021,339820,1888574,83.18,80.18
union
select 1039109, 2021,339820,1888575,97.05,99.00
union
select 1039109, 2021,339820,1888576,92.87,94.87
union
select 1039109, 2021,339820,1888577,95.74,96.74
select * from [dbo].[TempMaterial]
Expected result as below
CompanyID | Year | Rev_ID | MetarialID1888574 | MetarialID1888575 | MetarialID1888576 | MetarialID1888577 | supplier1888574 | supplier1888575 | supplier1888576 | supplier1888577 |
---|---|---|---|---|---|---|---|---|---|---|
1039109 | 2021 | 339820 | 83.18 | 97.05 | 92.87 | 95.74 | 80.18 | 99.00 | 94.87 | 96.74 |
1039152 | 2020 | 339898 | 80.18 | 100 | 98.87 | 93.74 | 79.18 | 99.00 | 96.87 | 90.74 |
so can you help me make it dynamically please for both column