sourabh choubey

sourabh choubey

  • NA
  • 174
  • 46k

dynamiv pivot in sql server

Jul 12 2016 2:38 AM
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..

Answers (23)