您可以两次加入FactTable以分别获取以前和当前数据。
在BridgeTable中不需要Datekey和/或PYdatekey的情况下使用LEFT JOIN。
--Sample data
CREATE TABLE BridgeTable (Datekey int, PYdatekey int)
CREATE TABLE FactTable (datekey int, SomeField varchar(100))
INSERT INTO FactTable VALUES (1, 'A')
INSERT INTO FactTable VALUES (2, 'B')
INSERT INTO BridgeTable VALUES (1, 2)
INSERT INTO BridgeTable VALUES (1, null)
INSERT INTO BridgeTable VALUES (null, 2)
--Query
SELECT PreviousFacts.SomeField AS Previous_SomeField, CurrentFacts.SomeField AS Current_SomeField --Or whatever columns you need
FROM BridgeTable
LEFT JOIN FactTable PreviousFacts ON BridgeTable.PYdatekey = PreviousFacts.DateKey
LEFT JOIN FactTable CurrentFacts ON BridgeTable.Datekey = CurrentFacts.DateKey
'INNER JOIN' - [使用内部联接(https://technet.microsoft.com/fi-fi/library/ms190014(V = SQL.105)的.aspx) – Fabio
而不是描述,只是具有期望的输出结构的条目的后表结构。还显示,你仍然做了什么。 – Ajay2707