我有一个表格可以计算每人的数字总和。然后查看该表以获取该数字,并将其添加到另一组数据中。如果我只将其链接到该列,但它需要链接到 2 个可能的列,则执行此操作的左连接将起作用,并且当我添加
or
部分以链接到第二列时,它会超时。
查找表 ENT.Sessions:
Select
pupilonrolltableid,
pupilnolongeronrolltableid,
sum(sessions) As Sessions
From CensusAttendance
Where attendancereason NOT LIKE '%[/\BDJLPSVWXY#]%'
Group By pupilnolongeronrolltableid, pupilonrolltableid
我正在使用两列;由于两者都不会被填充,因此将是其中之一,即
这是为新表构建数据的 SQL,我在其中使用左连接从 Sessions 表中获取数据。
人口普查表:
Select
Coalesce(ONR.forename,OFR.forename) As Forename
,Coalesce(ONR.middlenames,OFR.middlenames) As Middlenames
,Coalesce(ONR.surname,OFR.surname) As Surname
,Coalesce(ONR.upn,OFR.upn) As UPN
,Coalesce(ONR.[schoolcensustableid],OFR.[schoolcensustableid]) As SchoolCensusTableID
,CSC.term As Term
,CSC.[year] As [Year]
,Case When TSO.Sessions IS NULL Then Cast('0.00' As Decimal(10,2)) Else TSO.Sessions END As SessionsAuthorised
,Case When ONR.termlysessionspossible IS NULL Then Cast('0.00' As Decimal(10,2)) Else ONR.termlysessionspossible END As SessionsPossibleOnRoll
,Case When OFR.termlysessionspossible IS NULL Then Cast('0.00' As Decimal(10,2)) Else OFR.termlysessionspossible END As SessionsPossibleOffRoll
,ONR.termlysessionseducational As TermlySessionsEducationalOnRoll
,OFR.termlysessionseducational As TermlySessionsEducationalOffRoll
,ONR.termlysessionsexceptional As TermlySessionsExceptionalOnRoll
,OFR.termlysessionsexceptional As TermlySessionsExceptionalOffRoll
,ONR.termlysessionsauthorised As TermlySessionsAuthorisedOnRoll
,OFR.termlysessionsauthorised As TermlySessionsAuthorisedOffRoll
,ONR.termlysessionsunauthorised As TermlySessionsUnauthorisedOnRoll
,OFR.termlysessionsunauthorised As TermlySessionsUnauthorisedOffRoll
,Coalesce (ONR.DateTimeInserted, OFR.DateTimeInserted) As DateTimeInserted
,Coalesce (ONR.DateTimeUpdated,OFR.DateTimeUpdated) As DateTimeUpdated
,Coalesce (ONR.MD5Checksum, OFR.MD5Checksum) As MD5Checksum
,Coalesce (ONR.LoadID, OFR.LoadID) As LoadID
,Coalesce (ONR.UniversalID, OFR.UniversalID) As UniversalID
From STG.Census_PupilOnRoll As ONR
Full Outer Join STG.Census_PupilNoLongerOnRoll As OFR
On ONR.schoolcensustableid = OFR.schoolcensustableid
And ONR.upn = OFR.upn
Left Outer Join STG.Census_SchoolCensus As CSC On ONR.schoolcensustableid = CSC.schoolcensustableid
Or OFR.schoolcensustableid = CSC.schoolcensustableid
Left Outer Join ENT.CensusSessions As TSO On TSO.pupilnolongeronrolltableid = OFR.pupilnolongeronrolltableid
Or TSO.pupilonrolltableid = ONR.pupilonrolltableid
如果我将最后一个左外连接留在第一个
On
=
,它将加载数据,但仅链接到会话中的某些数据,但是一旦我添加 Or
,它就永远不会加载。
如何让它发挥作用?
Session 中有超过 100 万条记录,构建其他数据集的 SQL 有 2-300 万条记录。
如果您将其构造为单独执行所有连接,您可能会拥有更好的运气:
with Census_PupilOnRoll As (
Select ONR.*, CSC.term, CSC."year", TSO.sessions
From STG.Census_PupilOnRoll As ONR
Left Outer Join STG.Census_SchoolCensus As CSC
On CSC.schoolcensustableid = ONR.schoolcensustableid
Left Outer Join ENT.CensusSessions As TSO
On TSO.pupilonrolltableid = ONR.pupilonrolltableid
), Census_PupilNoLongerOnRoll as (
Select OFR.*, CSC.term, CSC."year", TSO.sessions
From STG.Census_PupilNoLongerOnRoll As OFR
Left Outer Join STG.Census_SchoolCensus As CSC
On CSC.schoolcensustableid = OFR.schoolcensustableid
Left Outer Join ENT.CensusSessions As TSO
On TSO.pupilnolongeronrolltableid = OFR.pupilnolongeronrolltableid
)
Select * /* <-- your output columns go here */
From Census_PupilOnRoll As ONR
Full Outer Join Census_PupilNoLongerOnRoll As OFR
On ONR.schoolcensustableid = OFR.schoolcensustableid And ONR.upn = OFR.upn;