将日期范围分割成块,重新连接到一个表KDB+/Q中



我有一个像一样连接的表

result: select from table where date within (sd;ed)

其中sd和ed跨越多个月(如sd:2021.07.01 ed:2021.09.30(。如果你花了一个多月的时间,我从中查询的表会中断,所以为了得到我需要的结果,我必须做如下操作:

result: uj(uj(select from table where date within (2021.07.01;2021.07.30);select from table where date within (2021.08.01;2021.08.31));select from table where date within (2021.09.01;2021.09.30))

如何为任何sd和ed创建此动态?也就是说,我如何将时间范围分解为几个月的头几天和最后几天,并将它们干净地合并到一张表中?我最初的想法是将天数除以x时间量,由用户输入,然后将结果的天数添加到sd中以获得帧,但这变得很混乱。

像这样的东西应该会为您阻塞它:

raze{select from t where date within x}each(first;last)@:/:d group"m"$d:sd+til 1+ed-sd

不要像您建议的那样使用where date.month=x,至少不要用于历史查询

将开始日期和结束日期转换为可迭代日期列表的一个选项可能是:

f:{0N 2#(x,raze -1 0+/:`date$mx+1+til(`month$y)-mx:`month$x),y}

其中x是开始日期,y是结束日期。

f[2021.07.14;2022.02.09]
2021.07.14 2021.07.31
2021.08.01 2021.08.31
2021.09.01 2021.09.30
2021.10.01 2021.10.31
2021.11.01 2021.11.30
2021.12.01 2021.12.31
2022.01.01 2022.01.31
2022.02.01 2022.02.09

然后你可以运行:

{select from t where date within x} each f[sd;ed]

并使用raze(uj/)连接结果

最新更新