我遇到以下问题时遇到问题。我需要找到入学年份之间增加或减少的百分比。由于某种原因,我得到多个订单项,我的输出不正确。脚本运行正常但产生的结果不正确
脚本:
WITH cte
AS ( SELECT *
FROM ( SELECT DISTINCT
dt.YEAR ,
db.business_code ,
db.business_name ,
dt.time_key ,
fes.total_enrollment
FROM fact
AS fes
LEFT OUTER JOIN building
AS db ON fes.building_key = db.building_key
LEFT OUTER JOIN mart.time
AS dt ON fes.time_key = dt.time_key
) AS source PIVOT
( sum(total_enrollment) FOR [year] IN ( [2012], [2011], [2010], [2009],
[2008] ) ) as pvt
)
SELECT company_code ,
company_name ,
[2012] ,
( CASE WHEN [2011] <> 0
THEN CONVERT(DECIMAL(18, 2), ( [2012] - [2011] )
/ CONVERT(DECIMAL(18, 2), ( [2011] ))) * 100
END ) AS '% Degree -/+' ,
[2011] ,
( CASE WHEN [2010] <> 0
THEN CONVERT(DECIMAL(18, 2), ( [2011] - [2010] )
/ CONVERT(DECIMAL(18, 2), ( [2010] ))) * 100
END ) AS '% Degree -/+' ,
[2010] ,
( CASE WHEN [2009] <> 0
THEN CONVERT(DECIMAL(18, 2), ( [2010] - [2009] )
/ CONVERT(DECIMAL(18, 2), ( [2009] ))) * 100
END ) AS '% Degree -/+' ,
[2009] ,
( CASE WHEN [2008] <> 0
THEN CONVERT(DECIMAL(18, 2), ( [2009] - [2008] )
/ CONVERT(DECIMAL(18, 2), ( [2008] ))) * 100
END
) AS '% Degree -/+'
FROM cte
ORDER BY company_name
继承我的SQL输出:
这是我想要的输出:
感谢您的帮助......