我有一个现有表,其中有多家公司的ClosePrice数据。我只需要使用一些计算为特定公司(ABC)填充一个新列(CalcCol)。基本上,我需要找到当天(ABC)和另一家公司(XYZ)在当天和上一个交易日的收盘价。进行图像中所示的计算,并使用表中该公司(ABC)所有行的计算值填充CalcCol。我使用sql之类的东西来获取ABC和XYZ的closeprice和prevdaycloseprice
select tradedate, closeprice,PrevDayClosePrice from (
select tradedate, ClosePrice,
lag(closeprice,1) over (partition by TICKER order by datekey) as PrevDayClosePrice
from [dbo].[factStockDividendCommodityIndex] where TICKER='ABC') as query
我正在尝试使用游标进行遍历,计算并填充数据,但是填充时间过长,这是不可接受的。正在寻找有效解决此要求的想法。
如果要更新数据,请使用update
:
with toupdate as (
select tradedate, ClosePrice,
lag(closeprice, 1) over (partition by TICKER order by datekey) as PrevDayClosePrice
from [dbo].[factStockDividendCommodityIndex]
where TICKER in ('ABC', 'XYZ')
)
update toupdate
set PrevDayClosePrice = new_PrevDayClosePrice;