Hi @manish verma ,
Please refer below:
drop table if exists #temp1
select distinct *
into #temp1
from (
(select distinct year FROM mytable) a
cross apply (select distinct Period FROM mytable) b
cross apply (select 'ActualCost' measure union select 'netprofit' ) c)
declare @sql nvarchar(max)
declare @s1 nvarchar(max)
declare @s2 nvarchar(max)
declare @s3 nvarchar(max)
declare @s4 nvarchar(max)
declare @s5 nvarchar(max)
declare @s6 nvarchar(max)
declare @s7 nvarchar(max)
declare @s8 nvarchar(max)
select @s1=STUFF((select distinct ',['+ cast(year as char(4))+'-'+ measure+']' from #temp1 where measure='ActualCost'
FOR XML PATH('') ), 1, 1, '')
select @s2=STUFF((select distinct ',isnull([' +cast(year as char(4))+'-' +measure+'],0) ['+ cast(year as char(4))+ measure++']'
from #temp1 where measure='ActualCost' FOR XML PATH('') ), 1, 1, '')
select @s3=STUFF((select distinct ',['+ cast(year as char(4))+'-'+ measure+']' from #temp1 where measure='netprofit'
FOR XML PATH('') ), 1, 1, '')
select @s4=STUFF((select distinct ',isnull([' +cast(year as char(4))+'-' +measure+'],0) ['+ cast(year as char(4))+ measure++']'
from #temp1 where measure='netprofit' FOR XML PATH('') ), 1, 1, '')
select @s5=STUFF((select distinct ',['+ cast(year as char(4))+'-'+cast(Period as char(3))+'-'+ measure+']' from #temp1 where measure='ActualCost'
FOR XML PATH('') ), 1, 1, '')
select @s6=STUFF((select distinct ',isnull([' +cast(year as char(4))+'-'+cast(Period as char(3))+'-' +measure+'],0) ['+ cast(year as char(4))+'-'+cast(Period as char(3))+ measure++']'
from #temp1 where measure='ActualCost' FOR XML PATH('') ), 1, 1, '')
select @s7=STUFF((select distinct ',['+ cast(year as char(4))+'-'+cast(Period as char(3))+'-'+ measure+']' from #temp1 where measure='netprofit'
FOR XML PATH('') ), 1, 1, '')
select @s8=STUFF((select distinct ',isnull([' +cast(year as char(4))+'-'+cast(Period as char(3))+'-' +measure+'],0) ['+ cast(year as char(4))+'-'+cast(Period as char(3))+ measure++']'
from #temp1 where measure='netprofit' FOR XML PATH('') ), 1, 1, '')
set @sql=N'
SELECT * into #temp FROM
(
SELECT
Concat([Year],''-'',''ActualCost'') as [YearActualCost],
Concat([Year],''-'',''netprofit'') as [Yearnetprofit],
Concat([Year],''-'', [Period],''-'',''ActualCost'') as [Date],
Concat([Year],''-'', [Period],''-'',''netprofit'') as [netprofitDate],
Companyid,
Product,
ActualCost,
ActualCost as MonthActualCost,
netprofit,
netprofit as Monthnetprofit
FROM mytable
) t
PIVOT(Sum([ActualCost]) for [YearActualCost] in('+@s1+')) AS pivot_table0
PIVOT(Sum([netprofit]) for [Yearnetprofit] in('+@s3+')) AS pivot_table1
PIVOT(Sum([MonthActualCost]) for [Date] in('+@s5+')) AS pivot_table2
PIVOT(Sum([Monthnetprofit]) for [netprofitDate] in('+@s7+')) AS pivot_table3;
select Companyid,Product,'+@s2+','+@s4+','+@s6+','+@s8+'
from #temp'
EXECUTE sp_executesql @sql
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.