Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900