i have a pivot query which converts rows into column .But the problem is that i have to use that row value that i used in for columnname ([row1],[row2]).Now these row 1 and row2 become act as column but i want it dynamically.Suppose if i have four row value that i s row1,row2,row3,row4 and i want to make it column than i not need to write it as for columnname ([row1],[row2])....Please help me..My code is given below..
What I have tried:
select * from
(select dbo.CreditDebitExpenses.ID AS SLNO, TransactionDate, Particular,Expenses_Name,Debit_Expenses,Credit_Expenses,Expenses,Balance from dbo.CreditDebitExpenses inner join
dbo.CreditExpenses on dbo.CreditDebitExpenses.ID=dbo.CreditExpenses.Credit_ID
inner join dbo.Expenses on dbo.CreditExpenses.Expenses_ID=dbo.Expenses.ID )src
pivot
(
sum(Expenses)
for Expenses_Name In ([Electrical_Expenses],[Lift_Expenses],[BuildingMaintainence_Expenses],[Miscelleneous_Expenses])
)as pvt
i want [Electrical_Expenses],[Lift_Expenses],[BuildingMaintainence_Expenses],[Miscelleneous_Expenses] that is the records in expenses table having column name expenses_name is created dynamically no need to write as above..