我正在尝试使用之前的信息回填一列。像这样
BizID Quarter Value
4168 201501 GOLD
4168 201502 NULL
4168 201503 NULL
4168 201504 NULL
4168 201601 REGISTERED
4168 201602 NULL
4168 201603 NULL
4168 201604 NULL
我想要什么
BizID Quarter Value
4168 201501 GOLD
4168 201502 GOLD
4168 201503 GOLD
4168 201504 GOLD
4168 201601 REGISTERED
4168 201602 REGISTERED
4168 201603 REGISTERED
4168 201604 REGISTERED
我在Microsoft SQL Server 2014上运行。因此LAG()函数不起作用,LAST_VALUE()函数也不起作用。我尝试了这两个,导致相同的错误“Function()不是公认的内置函数名称。”
我的完整代码如下。我创建了3个单独的表。第一个[DummyTable]只列出了8个季度。第二个[历史]引入PartnerAttributevalues(这些仅在发生更改时填充)。第三个[最新]从2014年之前的记录中搜索最相关信息并填写最高行值的信息。
SELECT DummyTable.BusinessID
, DummyTable.Quarter
, CASE WHEN ((History.PartnerAttributeValue IS NULL) AND (DummyTable.Quarter = 201501)) THEN Latest.PartnerAttributeValue
ELSE History.PartnerAttributeValue
END AS PartnerAttributeValue
FROM
(SELECT BizID.BusinessID,Quart.Quarter
FROM(
(SELECT PPAV.BusinessID
FROM Partner_PartnerAttributeValue PPAV
WHERE PPAV.BusinessID = 4168
GROUP BY PPAV.BusinessID)BizID
CROSS JOIN
(SELECT * FROM
(VALUES ('201501', '2015-01-01','2015-03-31')
, ('201502', '2015-04-01','2015-06-30')
, ('201503', '2015-07-01','2015-09-30')
, ('201504', '2015-10-01','2015-12-31')
, ('201601', '2016-01-01','2016-03-31')
, ('201602', '2016-04-01','2016-06-30')
, ('201603', '2016-07-01','2016-09-31')
, ('201604', '2016-10-01','2016-12-31')) AS Quarters(Quarter,StartDate,EndDate)) Quart
)
)DummyTable
LEFT JOIN
(SELECT PPAV.BusinessID
, (cast(year(PPAV.PartnerAttributeValueStartDate) as char(4)) + '0' + cast(datepart(qq, PPAV.PartnerAttributeValueStartDate) as char(1))) AS Quarter
, PAV.PartnerAttributeValue
FROM Partner_PartnerAttributeValue PPAV
JOIN PartnerAttributeValue PAV
ON PAV.PartnerAttributeValueID = PPAV.PartnerAttributeValueID
WHERE PAV.PartnerAttributeID = 7
AND (PPAV.PartnerAttributeValueID = 22 OR PPAV.PartnerAttributeValueID = 795 OR PPAV.PartnerAttributeValueID = 796)
AND PPAV.BusinessID = 4168
GROUP BY PPAV.BusinessID
, (cast(year(PPAV.PartnerAttributeValueStartDate) as char(4)) + '0' + cast(datepart(qq, PPAV.PartnerAttributeValueStartDate) as char(1)))
, PAV.PartnerAttributeValue
)History
ON History.BusinessID = DummyTable.BusinessID
AND History.Quarter = DummyTable.Quarter
LEFT JOIN
(SELECT MAX(PPAV.PartnerAttributeValueStartDate) as MAXDate
, PAV.PartnerAttributeValue
, PPAV.BusinessID
FROM Partner_PartnerAttributeValue PPAV
JOIN PartnerAttributeValue PAV
ON PAV.PartnerAttributeValueID = PPAV.PartnerAttributeValueID
WHERE PAV.PartnerAttributeID = 7
AND (PPAV.PartnerAttributeValueID = 22 OR PPAV.PartnerAttributeValueID = 795 OR PPAV.PartnerAttributeValueID = 796)
AND (PPAV.BusinessID = 4168)
AND((cast(year(PPAV.PartnerAttributeValueStartDate) as char(4)) + '0' + cast(datepart(qq, PPAV.PartnerAttributeValueStartDate) as char(1))) < 201501)
GROUP BY PAV.PartnerAttributeValue
, PPAV.PartnerAttributeValueStartDate
, PPAV.BusinessID
)Latest
ON Latest.BusinessID = DummyTable.BusinessID
我正试图找出一种尽可能最有效地回填这些信息的方法。我提供的实例只查看了一个这样的实例。在更大的版本中,我将为成千上万的企业做这件事,因此效率是关键。
感谢您的帮助和指导!
答案 0 :(得分:0)
LAG()
和LAST_VALUE()
。您应该检查SQL Server兼容级别(请参阅here)。
在任何情况下,SQL Server都不支持IGNORE NULLS
的{{1}}选项,这是最简单的方法。
这是另一种方式:
LAG()
如果您想要就地更改数据,这很容易合并到select t.BizID, t.Quarter, t.Value, tval.value as new_value
from t outer apply
(select top 1 t2.*
from t2
where t2.BizID = t.BizId and t2.Value is not null and
t2.Quarter <= t.Quarter
order by t2.Quarter desc
) tval;
中。