感谢 TI 和 Martin 的快速响应。最初我没有发布完整的脚本,因为我只是对我的案例陈述有问题,我无法让 ISNULL 工作。以下是完整的结果和答案:
DECLARE @Data TABLE
(
[Year] INT ,
caucasian INT ,
Black INT ,
Asian INT ,
Native INT ,
Hispanic INT ,
[American Indian] INT ,
Multiracial INT ,
UNKNOWN INT ,
Male INT ,
Female INT ,
[Total Enrollment] INT
)
INSERT INTO @Data
SELECT dt.year AS Year ,
SUM(ISNULL(caucasian_enrollment, 0)) ,
SUM(ISNULL(black_or_african_american_enrollment, 0)) ,
SUM(ISNULL(asian_enrollment,0)) ,
SUM(ISNULL(native_hawaiian_pacific_islander_enrollment, 0)) ,
SUM(ISNULL(hispanic_enrollment, 0)) ,
SUM(ISNULL(american_indian_or_alaskan_ative_enrollment, 0)) ,
SUM(ISNULL(multiracial_enrollment, 0)) ,
SUM(ISNULL(unknown_ethnicity_enrollment, 0)) ,
SUM(ISNULL(male_enrollment, 0)) ,
SUM(ISNULL(female_enrollment, 0)) ,
SUM(ISNULL(Total_Enrollment, 0))
FROM mart.dbo.f_s AS fes
INNER JOIN time AS dt ON fes.time_key = dt.time_key
LEFT OUTER JOIN building AS db ON fes.building_key = db.building_key
WHERE year = '2012'
GROUP BY dt.year
UNION ALL
SELECT dt.year AS Year ,
SUM(ISNULL(caucasian_enrollment, 0)) ,
SUM(ISNULL(black_or_african_american_enrollment, 0)) ,
SUM(ISNULL(asian_enrollment,0)) ,
SUM(ISNULL(native_hawaiian_pacific_islander_enrollment, 0)) ,
SUM(ISNULL(hispanic_enrollment, 0)) ,
SUM(ISNULL(american_indian_or_alaskan_ative_enrollment, 0)) ,
SUM(ISNULL(multiracial_enrollment, 0)) ,
SUM(ISNULL(unknown_ethnicity_enrollment, 0)) ,
SUM(ISNULL(male_enrollment, 0)) ,
SUM(ISNULL(female_enrollment, 0)) ,
SUM(ISNULL(Total_Enrollment, 0))
FROM F5A_education_mart_IAK12.dbo.fact_enrollment_school AS fes
INNER JOIN dim_time AS dt ON fes.time_key = dt.time_key
LEFT OUTER JOIN dim_building AS db ON fes.building_key = db.building_key
WHERE year = '2011'
GROUP BY dt.year
UNION ALL
SELECT dt.year AS Year ,
SUM(ISNULL(caucasian_enrollment, 0)) ,
SUM(ISNULL(black_or_african_american_enrollment, 0)) ,
SUM(ISNULL(asian_enrollment,0)) ,
SUM(ISNULL(native_hawaiian_pacific_islander_enrollment, 0)) ,
SUM(ISNULL(hispanic_enrollment, 0)) ,
SUM(ISNULL(american_indian_or_alaskan_ative_enrollment, 0)) ,
SUM(ISNULL(multiracial_enrollment, 0)) ,
SUM(ISNULL(unknown_ethnicity_enrollment, 0)) ,
SUM(ISNULL(male_enrollment, 0)) ,
SUM(ISNULL(female_enrollment, 0)) ,
SUM(ISNULL(Total_Enrollment, 0))
FROM F5A_education_mart_IAK12.dbo.fact_enrollment_school AS fes
INNER JOIN dim_time AS dt ON fes.time_key = dt.time_key
LEFT OUTER JOIN dim_building AS db ON fes.building_key = db.building_key
WHERE year = '2010'
GROUP BY dt.year
UNION ALL
SELECT dt.year AS Year ,
SUM(ISNULL(caucasian_enrollment, 0)) ,
SUM(ISNULL(black_or_african_american_enrollment, 0)) ,
SUM(ISNULL(asian_enrollment,0)) ,
SUM(ISNULL(native_hawaiian_pacific_islander_enrollment, 0)) ,
SUM(ISNULL(hispanic_enrollment, 0)) ,
SUM(ISNULL(american_indian_or_alaskan_ative_enrollment, 0)) ,
SUM(ISNULL(multiracial_enrollment, 0)) ,
SUM(ISNULL(unknown_ethnicity_enrollment, 0)) ,
SUM(ISNULL(male_enrollment, 0)) ,
SUM(ISNULL(female_enrollment, 0)) ,
SUM(ISNULL(Total_Enrollment, 0))
FROM F5A_education_mart_IAK12.dbo.fact_enrollment_school AS fes
INNER JOIN dim_time AS dt ON fes.time_key = dt.time_key
LEFT OUTER JOIN dim_building AS db ON fes.building_key = db.building_key
WHERE year = '2009'
GROUP BY dt.year
UNION ALL
SELECT dt.year AS Year ,
SUM(ISNULL(caucasian_enrollment, 0)) ,
SUM(ISNULL(black_or_african_american_enrollment, 0)) ,
SUM(ISNULL(asian_enrollment,0)) ,
SUM(ISNULL(native_hawaiian_pacific_islander_enrollment, 0)) ,
SUM(ISNULL(hispanic_enrollment, 0)) ,
SUM(ISNULL(american_indian_or_alaskan_ative_enrollment, 0)) ,
SUM(ISNULL(multiracial_enrollment, 0)) ,
SUM(ISNULL(unknown_ethnicity_enrollment, 0)) ,
SUM(ISNULL(male_enrollment, 0)) ,
SUM(ISNULL(female_enrollment, 0)) ,
SUM(ISNULL(Total_Enrollment, 0))
FROM F5A_education_mart_IAK12.dbo.fact_enrollment_school AS fes
INNER JOIN dim_time AS dt ON fes.time_key = dt.time_key
LEFT OUTER JOIN dim_building AS db ON fes.building_key = db.building_key
WHERE year = '2008'
GROUP BY dt.year
--Race 2012 % inc/dec 2011 % inc/dec 2010
--Caucasian 3000 -.33 4000 .6 2500
--Black 1000 -.5 2000 .25 1500
--Asian 100 .5 50 .4 30
SELECT Race ,
[2012] = MAX(CASE WHEN a.[Year] = 2012 THEN [Count]
END) ,
[% Inc Dec] = ( MAX(CAST(CASE WHEN a.[Year] = 2011 THEN [Count]
END AS FLOAT))
- MAX(CAST(CASE WHEN a.[Year] = 2012 THEN [Count]
END AS FLOAT)) )
/ NULLIF(MAX(CAST(CASE WHEN a.[Year] = 2012 THEN [Count]
END AS FLOAT)),0) ,
[2011] = MAX(CASE WHEN a.[Year] = 2011 THEN [Count]
END) ,
[% Inc Dec] = ( MAX(CAST(CASE WHEN a.[Year] = 2010 THEN [Count]
END AS FLOAT))
- MAX(CAST(CASE WHEN a.[Year] = 2011 THEN [Count]
END AS FLOAT)) )
/ NULLIF(MAX(CAST(CASE WHEN a.[Year] = 2010 THEN [Count]
END AS FLOAT)),0) ,
[2010] = MAX(CASE WHEN a.[Year] = 2010 THEN [Count]
END) ,
[% Inc Dec] = ( MAX(CAST(CASE WHEN a.[Year] = 2009 THEN [Count]
END AS FLOAT))
- MAX(CAST(CASE WHEN a.[Year] = 2010 THEN [Count]
END AS FLOAT)) )
/ NULLIF(MAX(CAST(CASE WHEN a.[Year] = 2009 THEN [Count]
END AS FLOAT)),0) ,
[2009] = MAX(CASE WHEN a.[Year] = 2009 THEN [Count]
END) ,
[% Inc Dec] = ( MAX(CAST(CASE WHEN a.[Year] = 2008 THEN [Count]
END AS FLOAT))
- MAX(CAST(CASE WHEN a.[Year] = 2009 THEN [Count]
END AS FLOAT)) )
/ NULLIF(MAX(CAST(CASE WHEN a.[Year] = 2009 THEN [Count]
END AS FLOAT)), 0)
FROM @Data
CROSS APPLY ( VALUES
( 'Caucasian', caucasian, [Year])
, ( 'Black', Black, [Year])
, ( 'Asian', Asian, [Year])
, ('Native', Native, [Year])
, ('Hispanic', Hispanic, [Year])
, ('American Indian', [American Indian], [Year])
, ('Multiracial', multiracial, [Year])
, ('Unkown', unknown, [Year])
, ('Male', male, [Year])
, ('Female', female, [Year])
, ('Total Enrollment', [Total Enrollment], [Year])
) a ( Race, [Count], [Year] ) GROUP BY Race