TSQL Pivot

i tried to do pivot

SQL I tried
drop table #temp 
go 

select distinct Head_Name into #temp 
 from 
  tbl_employee  a join tbl_fse b 
on a.Emp_Code = b.Emp_Code join tbl_head c 
on b.Head_ID = c.Head_ID  
go 

select * from #temp 
go 

DECLARE @colsUnpivot AS NVARCHAR(MAX)
DECLARE @query AS NVARCHAR(MAX)

select @colsUnpivot 
  = stuff((select ','+quotename(head_name)
           from #temp 
           for xml path('')), 1, 1, '')
          
print @colsUnpivot 


set @query = 
'
SELECT *
FROM (
   select a.emp_code,a.emp_name,Head_Name,FSE_Amount from tbl_employee  a join tbl_fse b 
on a.Emp_Code = b.Emp_Code join tbl_head c 
on b.Head_ID = c.Head_ID
) as s
PIVOT
(
    SUM(FSE_Amount)
    FOR [Head_Name] IN ('+@colsUnpivot+') ) AS pvt'
    
  exec sp_executesql @query;
Output

So do you have a question for us??

Sorry
Scott

It was meant for somebody
I was helping on this forum

I should have possibly
Sent him a message on SQL team

Apologies
,