1

关于如何解决此查询依赖问题的任何想法?子查询有帮助吗?我使用的数据库是 sql server 2012。

FROM [Scheduling].[studentsection] AS [table027]  

Left JOIN [Grading].[StudentGradeBucket] AS [table028]
ON ([table028].[StudentSectionID] = [table027].[StudentSectionID])
    And (@0 = [table002].[label]) 

Left JOIN [Grading].[GradingPeriodGradeBucket] AS [table029]
ON [table028].[GradingPeriodGradeBucketID] = [table029].[GradingPeriodGradeBucketID] 

Left JOIN [Grading].[GradeBucket] AS [table002]  
ON [table029].[GradeBucketID] = [table002].[GradeBucketID] 

Left JOIN [Grading].[GradeBucketType] AS [table001]  
ON [table002].[GradeBucketTypeID] = [table001].[GradeBucketTypeID] 

Left JOIN [Grading].[GradeMark] AS [table022]  
ON [table028].[GradeMarkID] = [table022].[GradeMarkID] 

我遇到的依赖问题是:

@0 = [table002].[label] //@0 is a string variable

就像尚未创建联接,但我需要使用它来为关系 [Grading].[StudentGradeBucket] 或 [table028] 创建联接

4

2 回答 2

3

这是您正在使用的奇怪的循环连接逻辑,而您选择的这些表别名使其更加混乱。别名应该简化,而不是混淆。也就是说,我认为可以将其移至 where 子句:

...
Left JOIN [Grading].[GradeMark] AS [table022]  ON [table028].[GradeMarkID] = [table022].[GradeMarkID] 
where  @0 = [table002].[label]

如果失败了,你可能需要重新定义你的逻辑......这对我来说似乎有点循环。

于 2013-10-23T18:47:29.497 回答
0

尝试这个:

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
) 
于 2013-10-23T18:46:44.973 回答