我必须包含2017 - 2020年的数据。但是,对于某些记录,这些年没有数据。有没有办法填充它?
以下是示例表结构:
CREATE TABLE #Temp
(
RefLeaseAssumptionID VARCHAR(30),
Scenario VARCHAR(15),
RefBuildingID VARCHAR(15),
BuildingName Varchar(50),
TenantName varchar(50),
UnitID varchar(15),
StartDate date,
EndDate date,
Area int,
Years int,
GAAPRevenue int
)
INSERT INTO #Temp VALUES ('20400-TSPECTENB-LSPECTENB-0190','2017_B03', '20400', 'ABCD-123', 'New Tenant (spec)', '2700', '2019-05-01', '2024-04-30', 45788, 2019, 800000 )
INSERT INTO #Temp VALUES ('20400-TSPECTENB-LSPECTENB-0190','2017_B03', '20400', 'ABCD-123', 'New Tenant (spec)', '2700', '2019-05-01', '2024-04-30', 45788, 2020, 900000 )
INSERT INTO #Temp VALUES ('45000-TSPECTENB-LSPECTENB-0100','2017_B03', '45000', 'WXYZ-123', 'New Tenant (spec)', '0100', '2017-05-01', '2018-05-30', 20000, 2017, 300000 )
INSERT INTO #Temp VALUES ('45000-TSPECTENB-LSPECTENB-0100','2017_B03', '45000', 'WXYZ-123', 'New Tenant (spec)', '0100', '2017-05-01', '2018-05-30', 20000, 2018, 350000 )
INSERT INTO #Temp VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2018, 100000 )
INSERT INTO #Temp VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2019, 125000 )
INSERT INTO #Temp VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2020, 170000 )
结果我正在寻找:
请注意,#temp和#temp2之间的区别是缺少年份的额外行,GAAPRevenue为0。
CREATE TABLE #Temp2
(
RefLeaseAssumptionID VARCHAR(30),
Scenario VARCHAR(15),
RefBuildingID VARCHAR(15),
BuildingName Varchar(50),
TenantName varchar(50),
UnitID varchar(15),
StartDate date,
EndDate date,
Area int,
Years int,
GAAPRevenue int
)
INSERT INTO #Temp2 VALUES ('20400-TSPECTENB-LSPECTENB-0190','2017_B03', '20400', 'ABCD-123', 'New Tenant (spec)', '2700', '2019-05-01', '2024-04-30', 45788, 2017, 0 )
INSERT INTO #Temp2 VALUES ('20400-TSPECTENB-LSPECTENB-0190','2017_B03', '20400', 'ABCD-123', 'New Tenant (spec)', '2700', '2019-05-01', '2024-04-30', 45788, 2018, 0 )
INSERT INTO #Temp2 VALUES ('20400-TSPECTENB-LSPECTENB-0190','2017_B03', '20400', 'ABCD-123', 'New Tenant (spec)', '2700', '2019-05-01', '2024-04-30', 45788, 2019, 800000 )
INSERT INTO #Temp2 VALUES ('20400-TSPECTENB-LSPECTENB-0190','2017_B03', '20400', 'ABCD-123', 'New Tenant (spec)', '2700', '2019-05-01', '2024-04-30', 45788, 2020, 900000 )
INSERT INTO #Temp2 VALUES ('45000-TSPECTENB-LSPECTENB-0100','2017_B03', '45000', 'WXYZ-123', 'New Tenant (spec)', '0100', '2017-05-01', '2018-05-30', 20000, 2017, 300000 )
INSERT INTO #Temp2 VALUES ('45000-TSPECTENB-LSPECTENB-0100','2017_B03', '45000', 'WXYZ-123', 'New Tenant (spec)', '0100', '2017-05-01', '2018-05-30', 20000, 2018, 350000 )
INSERT INTO #Temp2 VALUES ('45000-TSPECTENB-LSPECTENB-0100','2017_B03', '45000', 'WXYZ-123', 'New Tenant (spec)', '0100', '2017-05-01', '2018-05-30', 20000, 2019, 0 )
INSERT INTO #Temp2 VALUES ('45000-TSPECTENB-LSPECTENB-0100','2017_B03', '45000', 'WXYZ-123', 'New Tenant (spec)', '0100', '2017-05-01', '2018-05-30', 20000, 2020, 0 )
INSERT INTO #Temp2 VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2017, 0 )
INSERT INTO #Temp2 VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2018, 100000 )
INSERT INTO #Temp2 VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2019, 125000 )
INSERT INTO #Temp2 VALUES ('25000-TSPECTENB-LSPECTENB-1','2017_B03', '25000', 'XZZZ-123', 'New Tenant (spec)', '0300', '2017-05-01', '2020-10-31', 10000, 2020, 170000 )
答案 0 :(得分:1)
这有一份所需年份的手工清单(年份CTE),并获得不同租约的清单(租赁CTE)。然后,您将交叉连接两个列表以获取所有必需的行,将左外连接添加到现有行,并忽略任何具有匹配项的行。此选择可以获取所有缺失的数据。
我添加了插入以将缺少的行添加回#Temp,但注释掉了该行。要实际添加数据,只需在运行之前取消注释该行。
如果您愿意,也可以用替换为内联子查询的CTE来编写。
With Years as
(Select 2017 as YearNo
UNION ALL Select 2018
UNION ALL Select 2019
UNION ALL Select 2020
), Leases as (
Select distinct RefLeaseAssumptionID, Scenario, RefBuildingID, BuildingName, TenantName, UnitID, StartDate, EndDate, Area
From #Temp1
)
--Insert into #Temp (RefLeaseAssumptionID, Scenario, RefBuildingID, BuildingName, TenantName, UnitID, StartDate, EndDate, Area, Year, GAAPRevenue
Select L.RefLeaseAssumptionID, L.Scenario, L.RefBuildingID, L.BuildingName, L.TenantName, L.UnitID, L.StartDate, L.EndDate, L.Area, Y.YearNo, 0
from Leases L
Cross Join Years Y
left outer join #Temp T1 on T1.RefLeaseAssumptionID = L.RefLeaseAssumptionID and T1.Years = Y.YearNo
Where T1.RefLeaseAssumptionID is Null;