次のようなフィールドを持つドキュメントテーブルがあります
DocumentID int - PK and autoincrement
LastStatusChangedDateTime - datetime
JurisdictionID - int
DocumentStatusID - int
という名前の計算列を取り込みたいと思いますCanChangeStatus
。これは行列のようなものです。そのため、ドキュメントが最初にある場合は、ステータスを変更できます。これは私のクエリです:
Select d.DocumentID,
(
Select Cast(Case When d.DocumentID = v.DocumentID Then 1 Else 0 End as bit)
From
(
Select Top 1 DocumentID
From Documents
Where JurisdictionID = d.JurisdictionID
And
DocumentStatusID = d.DocumentStatusID
Order By LastStatusChangedDateTime
) v
) as CanChangeStatus
From Documents d
と にインデックスがありJurisdictionID
ますDocumentStatusID
。犯人はサブクエリです - 私が計算しようとしている方法ですCanChangeStatus
。結合はサブクエリよりもほとんど高速であるため、これを高速化するか、このサブクエリを結合に変換する方法はありますか?