尝试这个:
declare @0 nvarchar(max) = 'label value'
select *
from [Scheduling].[studentsection] as ss
left join [Grading].[StudentGradeBucket] as sgb
on sgb.[StudentSectionID] = ss.[StudentSectionID]
inner join [Grading].[GradingPeriodGradeBucket] as gpgb
on gpgb.[GradingPeriodGradeBucketID] = sgb.[GradingPeriodGradeBucketID]
inner join [Grading].[GradeBucket] as gb
on gb.[GradeBucketID] = gpgb.[GradeBucketID]
and gb.[label] = @0
left join [Grading].[GradeBucketType] as gbt
on gbt.[GradeBucketTypeID] = gb.[GradeBucketTypeID]
left join [Grading].[GradeMark] as gm
on gm.[GradeMarkID] = sgb.[GradeMarkID]
或者如果你真的想要左外连接:
declare @0 nvarchar(max) = 'label value'
select *
from [Scheduling].[studentsection] as ss
left join [Grading].[StudentGradeBucket] as sgb
on sgb.[StudentSectionID] = ss.[StudentSectionID]
left join [Grading].[GradingPeriodGradeBucket] as gpgb
on gpgb.[GradingPeriodGradeBucketID] = sgb.[GradingPeriodGradeBucketID]
left join [Grading].[GradeBucket] as gb
on gb.[GradeBucketID] = gpgb.[GradeBucketID]
and gb.[label] = @0
left join [Grading].[GradeBucketType] as gbt
on gbt.[GradeBucketTypeID] = gb.[GradeBucketTypeID]
left join [Grading].[GradeMark] as gm
on gm.[GradeMarkID] = sgb.[GradeMarkID]
或者,如果您需要在有匹配的 GradeBucket 记录时仅从 StudentGradeBucket 返回结果的逻辑;这个:
declare @0 nvarchar(max) = 'label value'
select *
from [Scheduling].[studentsection] as ss
left join [Grading].[StudentGradeBucket] as sgb
on sgb.[StudentSectionID] = ss.[StudentSectionID]
left join [Grading].[GradingPeriodGradeBucket] as gpgb
on gpgb.[GradingPeriodGradeBucketID] = sgb.[GradingPeriodGradeBucketID]
left join [Grading].[GradeBucket] as gb
on gb.[GradeBucketID] = gpgb.[GradeBucketID]
left join [Grading].[GradeBucketType] as gbt
on gbt.[GradeBucketTypeID] = gb.[GradeBucketTypeID]
left join [Grading].[GradeMark] as gm
on gm.[GradeMarkID] = sgb.[GradeMarkID]
where
( --filter on the gb label only if there's a result from the sgb table;
sgb.[StudentSectionID] is null
or and gb.[label] = @0
)