My Sql Query is :
select * from tblFeesDetail
DECLARE @DynamicPivotQuery AS NVARCHAR(MAX)
DECLARE @ColumnName AS NVARCHAR(MAX)
SELECT @ColumnName = ISNULL(@ColumnName + ',','')+ QUOTENAME(FeesDetail) FROM (SELECT DISTINCT FeesDetail FROM tblFeesDetail) AS FeesHead
SET @DynamicPivotQuery = ';WITH CTE AS(SELECT fd.FeeCatID,ClassName,AdmissionNo,ReferenceNo,SName,FName,FeeCatName,FeesDetail,Amount FROM tblFeesDetail as fd inner join tblFeesCategory as fc on fd.FeeCatID=fc.FeeCatID inner join tblStdReg as sr on sr.ClassID=fc.ClassID inner join tblDefClass as dc on fc.ClassID=dc.ClassID)
SELECT FeeCatID,FeeCatName,ClassName,AdmissionNo,ReferenceNo,SName,FName,'+@ColumnName+'FROM CTE
PIVOT (MAX(Amount)
FOR FeesDetail IN('+@ColumnName+')) p
WHERE FeeCatID = ''5''
ORDER BY FeeCatID DESC'
EXEC(@DynamicPivotQuery);