2013-10-23 159 views
1

有關如何解決此查詢依賴性問題的任何想法?子查詢會有幫助嗎?我使用的數據庫是SQL Server 2012的SQL多重左加入子查詢?

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 

像加入尚未創建,但我需要用它來創建聯接的關係[評分] [StudentGradeBucket]或[table028]

+3

您需要對您的問題進行更具體的描述......您嘗試解決哪些依賴性問題? (側面說明...這是最古怪的表格別名我已經看到編碼一段時間) – Twelfth

+0

我更新了我的帖子,更詳細。讓我知道你是否需要更多。 – user2529249

+0

什麼是@ 0 - 你是指其中一個表中的一列,還是它是一個變量/參數?你使用的是哪個數據庫? – JohnLBevan

回答

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 
) 
3

這是奇怪的圓形連接使用的是邏輯和你選擇是使它更加混亂,這些表的別名的。別名應該簡化,不要混淆。這麼說,我覺得它可以只被移動到WHERE子句:

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

如果失敗,則可能需要重新定義你的邏輯......似乎有點圓了我。

+0

這是不是會返回更多的記錄,然後必要的性能也會受到打擊? – user2529249

+0

使用此方法進行表格別名的原因是由於標識符的最大長度爲128個字符。我們有很深的關係,當它超過128個字符時,它就無法工作。所以這是解決這個問題的方法。基本上這是通過數據層(ORM)生成的,並且在生成sql時,實際的表名稱作爲註釋包含在頂部。 – user2529249

+0

這不是完整的查詢,如果你有興趣,我可以給你發送完整的查詢。我想我們將嘗試使用通用表格表達式(CTE)http://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx – user2529249