Firstly I must admit I learned t-sql on my own and my syntax I know is not the best as I use temp tables, but other than that, I am stumped. Every time I make a small change to fix one error, I get 3 more. It's been 4 hours and I just cannot get it to work.
Here is the temp table I've created in my sp where it references other temp tables in my sp (I know it's not the best way to do this but I digress):
Create Table #SG_MonthlyTotals(
GLAcct varchar(12),
GLDescription varchar(50),
AcctType varchar(5),
MainAccount varchar(5),
Location varchar(2),
Division varchar(3),
ContractType varchar(10),
Category varchar(50),
SubCategory varchar(50),
Mth date,
MonthlyBalance money,
MonthlyBudget money,
MonthlyBalancePY money
)
insert into #SG_MonthlyTotals
select a.*,m.Mth,m.MonthlyBalance,b.MonthlyBudget,p.MonthlyBalancePY
from #SG_GLAccounts a left outer join #SG_MonthlyBalances m
on a.GLAcct=m.GLAcct left outer join #SG_MonthlyBudgets b
on m.GLAcct=b.GLAcct and m.Mth=b.Mth left outer join #SG_MonthlyBalancesPY p
on a.GLAcct=p.GLAcct
I have 1 date parameters for the sp: @StartDate DateTime
I want to pivot 12 months of data and aggregate the MonthlyBalance amount. I need all the other columns then have the MonthlyBalance pivot for each month. The months are Dynamics, so I've used DateAdd to start at the @StartDate then add 11 months. I've done it the long way but it just makes more sense to me.
I tried it without the Convert and received an error but now still getting these 2 errors. I've tried everything. After some changes I get rid of the convert error but then get an error about the Mth being referenced twice. The Mth is the field I want to Pivot. The values in the db are DateTime and it has 2015-01-01 (the year and month change but the day is always 1 for every record so that's why I was adding a month).
Please help if you can. I had to put the [] around the dates at one point due to some other error.
Msg 8114, Level 16, State 1, Procedure SG_FinancialReports_Budgets_YTD, Line 107
Error converting data type nvarchar to date.
Msg 473, Level 16, State 1, Procedure SG_FinancialReports_Budgets_YTD, Line 107
The incorrect value "Convert(date,@StartDate)" is supplied in the PIVOT operator.
My code:
select *,Convert(date,@StartDate),DateAdd(month,1,Convert(date,@StartDate)),
DateAdd(month, 2, Convert(date,@StartDate)),DateAdd(month, 3, Convert(date,@StartDate)),
DateAdd(month, 4, Convert(date,@StartDate)),DateAdd(month, 5, Convert(date,@StartDate)),
DateAdd(month, 6, Convert(date,@StartDate)),DateAdd(month, 7, Convert(date,@StartDate)),
DateAdd(month, 8, Convert(date,@StartDate)),DateAdd(month, 9, Convert(date,@StartDate)),
DateAdd(month, 10, Convert(date,@StartDate)),DateAdd(month, 11, Convert(date,@StartDate))
from
(select Mth, MonthlyBalance from #SG_MonthlyTotals) as src
PIVOT
(
sum(MonthlyBalance)
For Mth
IN
(
[Convert(date,@StartDate)],[DateAdd(month, 1, Convert(date,@StartDate))],
[DateAdd(month, 2, Convert(date,@StartDate))],[DateAdd(month, 3, Convert(date,@StartDate))],
[DateAdd(month, 4, Convert(date,@StartDate))],[DateAdd(month, 5, Convert(date,@StartDate))],
[DateAdd(month, 6, Convert(date,@StartDate))],[DateAdd(month, 7, Convert(date,@StartDate))],
[DateAdd(month, 8, Convert(date,@StartDate))],[DateAdd(month, 9, Convert(date,@StartDate))],
[DateAdd(month, 10, Convert(date,@StartDate))],[DateAdd(month, 11, Convert(date,@StartDate))]
)
)
as PVT