我已经在这个脚本上工作了一段时间,但已经走到了死胡同。该脚本有效,但不幸的是产生了重复。我的脚本在 state_issue_teacher_id 键上跨数据库连接两个不同的表,然后生成输出。我检查了两个表并且行数相同,并且连接应该与记录完美匹配,但显然我的键或我加入表的方式存在问题,并且我的输出返回部分不正确。我还尝试连接属性以创建唯一键并加入表,但仍然产生不正确的结果。
这是我的脚本:
SELECT
LTRIM(RTRIM(rt.year_time)) AS 'year_time' ,
LTRIM(RTRIM(rt.state_issue_teacher_id)) AS state_issue_teacher_id ,
LTRIM(RTRIM(rt.district_code)) AS district_code ,
rt.district_name ,
rt.school_name ,
LTRIM(RTRIM(rt.assignment_code)) AS assignment_code ,
rt.assignment_desc ,
LTRIM(RTRIM(rt.position_code)) AS position_code ,
rt.position_desc ,
LTRIM(RTRIM(rt.last_name)) AS last_name ,
LTRIM(RTRIM(rt.first_name)) AS first_name ,
LTRIM(RTRIM(rt.total_salary)) AS total_salary ,
rt.assign_fte ,
LTRIM(RTRIM(rt.school_code)) AS school_code ,
rt.fte
FROM staging.dbo.rt AS rt
LEFT JOIN ( SELECT LTRIM(RTRIM(dti.year)) AS year ,
LTRIM(RTRIM(dt.teacher_id)) AS teacher_id ,
LTRIM(RTRIM(db.district_code)) AS district_code ,
db.district_name ,
LTRIM(RTRIM(dt.last_name)) AS last_name ,
LTRIM(RTRIM(dt.first_name)) AS first_name ,
LTRIM(RTRIM(da.assignment_code)) AS assignment_code ,
LTRIM(RTRIM(dp.position_code)) AS position_code ,
dre.race_ethnicity_code ,
LTRIM(RTRIM(SUBSTRING(db.school_code,10,4))) AS school_code ,
da.assignment_desc ,
dp.position_desc ,
fs.total_fte
FROM mart.dbo.fact_s AS fs
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_building
AS db ON fs.building_key = db.building_key
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_teacher
AS dt ON fs.teacher_key = dt.teacher_key
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_assignment
AS da ON fs.assignment_key = da.assignment_key
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_race_ethnicity
AS dre ON dt.race_ethnicity_key = dre.race_ethnicity_key
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_gender
AS dg ON dt.gender_key = dg.gender_key
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_time
AS dti ON fs.time_key = dti.time_key
LEFT OUTER JOIN mart.dbo.fact_s.dbo.dim_position
AS dp ON fs.position_key = dp.position_key
WHERE dti.year = '2012'
) raw ON rt.state_issue_teacher_id = raw.teacher_id
AND rt.year_time = raw.year
AND rt.last_name = raw.last_name
AND rt.first_name = raw.first_name
AND rt.district_code = raw.district_code
AND rt.position_code = raw.position_code
AND rt.school_code = RAW.school_code
AND rt.assignment_code = raw.assignment_code
WHERE rt.year_time = '2012'
ORDER BY rt.last_name, rt.first_name
我得到的输出是:
组合教师作业的 fte 应加起来为 1。但是具有相同 assignment_code/desc 和多个部分作业的教师会产生重复。示例:Jane Doe 出现 4 次,总 fte 为 2.0,而不是 2 次,正确的总 fte 为 1.0。输出应如下所示。