如何将一条记录根据相邻两条记录时间间隔填充或扩容为多条记录?

attachments-2022-09-tty43oBU631b044e2b55a.png

请先 登录 后评论

2 个回答

Juntao Wang

示例代码如下:

def f(t, i) {
	indCode = t[i][`indCode]
	indexCode = t[i][`indexCode]
	code = t[i][`code]
	dateStart = t[i][`date]
	dateEnd = t[i+1][`date]
	weight = t[i][`weight]
	totalMV = t[i][`totalMV]
	pholding = t[i][`pholding]
	jr = t[i][`jr]

	if(isNull(dateEnd)) { days = dateStart } // 表示最后一条
	else { days = dateStart..(dateEnd - 1) }
	days = days[weekday(days) between 1:5]
	return cj(table(indCode as indCode, indexCode as indexCode, code as code, weight as weight, totalMV as totalMV, pholding as pholding, jr as jr), table(days as date))
}

t = table(`35412`35412`35412 as indCode, `H30356.SH`H30356.SH`H30356.SH as indexCode, `000001.SZ`000001.SZ`000001.SZ as code, 2014.06.30 2014.09.30 2014.12.31 as date, 0.0168 0.0153 0.0158 as weight, 1111111111 2222222222 3333333333 as totalMV, 0.0015 0.0013 0.0009 as pholding, 1 1 1 as jr)
unionAll(each(f{t}, 1..size(t) - 1), false)

转换前:

attachments-2022-09-fCn4IcFG631b04c7f1642.png

转换后(部分数据展示):

attachments-2022-09-APsgbolj631b04e3a4e49.png

关联问题:如何将一条数据根据起始、终止日期填充或扩容为多条记录?

请先 登录 后评论
Huikang Xu

可以使用 interval 填充达到上述效果,代码如下:

t = table(`35412`35412`35412 as indCode, `H30356.SH`H30356.SH`H30356.SH as indexCode, 
          `000001.SZ`000001.SZ`000001.SZ as code, 2014.06.30 2014.09.30 2014.12.31 as date, 
          0.0168 0.0153 0.0158 as weight, 1111111111 2222222222 3333333333 as totalMV, 
          0.0015 0.0013 0.0009 as pholding, 1 1 1 as jr)
tmp = select indCode,indexCode,code,date,weight,totalMV,pholding,jr from t group by interval(date,1d,'prev') as date
// 去除非交易日数据
res = select * from tmp where date in getMarketCalendar("CFFEX")
请先 登录 后评论