2017-04-13 22 views
0

ここでは私のクエリですが、なぜ各行が結果を3倍にしているのかわかりません。私は別のものを選択しようとしました。 AssessmentProficiencyLevelRankはすべてのレコードに対して3回繰り返されます。これらの余分な行を削除するにはどうすればよいですか?SQLの繰り返し行

Select distinct 
sai.AcademicYear, 
sai.AcademicYearNum, 
sai.StateCode, 
sai.StudentBKID, 
sai.StudentDWID, 
sai.LastName, 
sai.FirstName, 
sai.ReportName, 
sai.calendarperiodname, 
case when aps.StateAssessmentProficiencySubjectName = 'Not Applicable' and sub.Subject = 'Algebra I' then 'Math' 
    when aps.StateAssessmentProficiencySubjectName = 'Not Applicable' and sub.Subject = 'Algebra 1' then 'Math' 
    when aps.StateAssessmentProficiencySubjectName = 'Not Applicable' and sub.Subject = 'Algebra II' then 'Math' 
    when aps.StateAssessmentProficiencySubjectName = 'Reading' then 'ELA' 
else aps.[StateAssessmentProficiencySubjectName] end as StateAssessmentProficiencySubjectName, 
sai.AssessmentName, 
sai.assessmentdate, 
sai.scaledscore, 
sai.GradeAbbreviation, 
dar.proficiency, 
dar.AssessmentProficiencyLevelRank 
into #main 

From StudentAssessmentMart.dbo.vwStateAssessmentInformation sai 

inner join StudentAssessmentMart.dbo.FctAssessmentStateSubject fct 
    on sai.StudentDWID = fct.StudentDWID 

inner join StudentAssessmentMart.dbo.DimStateAssessmentProficiencySubject aps 
    on fct.StateAssessmentProficiencySubjectDMID = aps.StateAssessmentProficiencySubjectDMID 

inner join StudentAssessmentMart.dbo.DimAssessmentSubject sub 
    on sub.AssessmentSubjectDWID = fct.AssessmentSubjectDWID 

inner join StudentAssessmentMart.dbo.DimAssessmentResults dar 
    on dar.AssessmentScoreCategoryDWID = fct.AssessmentScoreCategoryDWID 

where sai.Proficiency != 'Unknown Proficiency' 
and dar.Proficiency != 'Unknown Proficiency' 
and sai.StudentBKID = 174957 
and sai.Subject not in ('Science','Social Studies','SocStud','Listening','writing','sci','citizenship') 
and sai.gradeabbreviation not in ('k','1','2','unknown') 
order by StudentBKID 

select distinct min(AcademicYearNum) 'min', max(AcademicYearNum)'max', StudentBKID 
into #new 
from #main 
group by StudentBKID 

select distinct *, 'min year' as minmaxyear from #main main 
inner join #new newtable 
    on newtable.StudentBKID = main.StudentBKID 
    and newtable.min=main.AcademicYearNum 

where StateAssessmentProficiencySubjectName <> 'Not Applicable' 

union 

select distinct *, 'max year' as minmaxyear from #main main 
inner join #new newtable 
    on newtable.StudentBKID = main.StudentBKID 
    and newtable.max=main.AcademicYearNum 
and GradeAbbreviation = '8' 

where StateAssessmentProficiencySubjectName <> 'Not Applicable' 
order by main.StudentBKID 
drop table #main, #new 
+0

これを解決するための適切な方法を提供するには、さらに詳しい情報が必要です。最も重要なのは、dar.AssessmentProficiencyLevelRankのデータ型は何ですか? –

答えて

0

重複を取得しなくなるまで、一度に1つずつ結合をコメントアウトします。その後、重複を引き起こさないようにその結合を書き直すことに努力します。

+0

それを見つけました!ありがとうございました! – vanstrka