This is short query and hope you will understand...
WITH UnPvt AS (
SELECT * FROM (
SELECT Code,
DATENAME(MM, ForecastDate) MonthNameVal
,SUM(BOBLets) BOBLets/*int*/, SUM(BOBRevenue) BOBRevenue/*Decimal(18,2)*/,
Cast((sum(BOBRevenue)/sum(BOBLets)) as Decimal(18,2)) adr /*Need to calculate here*/
FROM (
SELECT Code, ForecastDate
,CAST(BOBLets AS Decimal(18,2)) BOBLets,BOBRevenue
FROM VW_DailyForecast
) v
GROUP BY Code, DATENAME(MM, ForecastDate)
) Sub
UNPIVOT
(
Vals FOR RowValues IN (BOBLets, BOBRevenue,adr)
) AS UnPvt
)
SELECT *
FROM UnPvt
PIVOT (
SUM(Vals)
FOR MonthNameVal IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November], [December])
) AS pvt
ORDER BY Code, RowValues desc
Error: Error:The type of column "adr" conflicts with the type of other columns specified in the UNPIVOT list.
Simple Table Schema:
ForecastDate ---Datetime
BOBLets --- int
BobRevenue --- Decimal
Code --- Varchar(20)
OK, I found the problem. The line CAST(SUM()/SUM() AS DECIMAL(18,2)) is different to the other sums.
Cast all you top values to DECIMAL(18,2)
Something like this
;WITH UnPvt AS (
SELECT * FROM (
SELECT Code,
DATENAME(MM, ForecastDate) MonthNameVal ,
CAST(SUM(LastYearLets) AS DECIMAL(18,2)) LastYearRooms,
CAST(SUM(LastYearRevenue) AS DECIMAL(18,2)) LastYearRevenue,
CAST(SUM(LastYearRevenue)/SUM(LastYearLets) AS DECIMAL(18,2)) LastYearADR,
CAST(SUM(BudgetLets) AS DECIMAL(18,2)) BudgetLets,
CAST(SUM(BudgetRevenue) AS DECIMAL(18,2)) BudgetRevenue,
CAST(SUM(FCastLets) AS DECIMAL(18,2)) FcastLets,
CAST(SUM(FCastRevenue) AS DECIMAL(18,2)) FCastRevenue,
CAST(SUM(BOBLets) AS DECIMAL(18,2)) BOBLets,
CAST(SUM(BOBRevenue) AS DECIMAL(18,2)) BOBRevenue
FROM (
SELECT Code,
ForecastDate ,
CAST(LastYearLets AS Decimal(18,2)) LastYearLets,
CAST(LastYearRevenue AS DECIMAL(18,2)) LastYearRevenue,
CAST(BudgetLets AS Decimal(18,2)) BudgetLets,
BudgetRevenue ,
CAST(FcastLets AS Decimal(18,2)) FcastLets,
FCastRevenue ,
CAST(BOBLets AS Decimal(18,2)) BOBLets,
BOBRevenue
FROM VW_DailyForecast
where forecastYear=2009
AND hotelID=1
AND SegmentID=1
) v
GROUP BY Code,
DATENAME(MM, ForecastDate)
) Sub
UNPIVOT
(
Vals FOR RowValues IN (LastYearRooms,LastYearRevenue,LastYearADR,BudgetLets,BudgetRevenue,FcastLets,FCastRevenue,BOBLets, BOBRevenue)
) AS UnPvt
)
SELECT *
FROM UnPvt
PIVOT (
SUM(Vals)
FOR MonthNameVal IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November], [December])
) AS pvt
ORDER BY Code, RowValues desc
Final Query
WITH UnPvt AS (
SELECT * FROM (
SELECT Code, DATENAME(MM, ForecastDate) MonthNameVal
,CAST(SUM(LastYearLets) as decimal(18,2)) LastYearRooms ,CAST(SUM(LastYearRevenue) as Decimal(18,2)) LastYearRevenue
,CAST((SUM(LastYearRevenue)/SUM(LastYearLets)) as decimal(18,2)) LastYearADR
,CAST(SUM(BudgetLets) as Decimal(18,2)) BudgetRooms, CAST(SUM(BudgetRevenue) as decimal(18,2)) BudgetRevenue
,CAST((SUM(BudgetRevenue)/SUM(BudgetLets)) as decimal(18,2)) BudgetADR
,CAST(SUM(FCastLets)as decimal(18,2)) FcastRooms, CAST(SUM(FCastRevenue)as decimal(18,2)) FCastRevenue
,CAST((SUM(FCastRevenue)/SUM(FCastLets)) as decimal(18,2)) FCastADR
,CAST(SUM(BOBLets)as decimal(18,2)) BOBRooms, CAST(SUM(BOBRevenue)as decimal(18,2)) BOBRevenue
,CAST((SUM(BOBRevenue)/SUM(BOBLets)) as decimal(18,2)) BOBADR
FROM (
SELECT Code, ForecastDate
,LastYearLets,LastYearRevenue ,LastYearADR,BudgetLets,BudgetRevenue, BudgetADR
,FcastLets,FCastRevenue, FCastADR, BOBLets, BOBRevenue, BOBADR
FROM VW_DailyForecast where forecastYear=2009 AND hotelID=1 AND SegmentID=1
) v
GROUP BY Code,
DATENAME(MM, ForecastDate)
) Sub
UNPIVOT
(
Vals FOR RowValues IN (LastYearRooms,LastYearRevenue,LastYearADR,BudgetRooms,BudgetRevenue,BudgetADR,FcastRooms,FCastRevenue,FCastADR,BOBRooms, BOBRevenue,BOBADR)
) AS UnPvt
)
SELECT *
FROM UnPvt
PIVOT (
SUM(Vals)
FOR MonthNameVal IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November], [December])
) AS pvt
ORDER BY Code, RowValues desc