你如何从KDB中的函数选择/更新/删除语句中获取日期范围?

huangapple go评论51阅读模式
英文:

How can I get a date range from a functional select/update/delete statement in KDB?

问题

我有以下功能性语句

    a. ?[tab;enlist (within;`date;(enlist;(-;`.z.D;5);`.z.D));0b;()]

    b. ?[tab;enlist (>;`date;(-;`.z.D;5));`.z.D));0b;()]

    c. ?[tab;enlist (in;`date;2023.03.30 2023.03.31);`.z.D));0b;()]

我想要做的是这样

```.util.getDateRange wc```(其中wc可以是上述功能语句中的一个where子句),我希望输出为

```2023.03.26 2023.03.31``` 对于 ```a```


```2023.03.30 2023.03.31``` 对于 ```c```

等等

有许多复杂情况例如您有人输入 ```select from tab where date>2023.03.15```)。

我想为了避免这些复杂情况我可以将where子句保存到一个变量 ```wc```然后做类似于

```?[([]date:.z.D + neg til .z.D-1950.01.01);wc;();`date]```

...但是我很羞于将这样的代码投入到生产环境中
英文:

I have the following functional statements:

a. ?[tab;enlist (within;`date;(enlist;(-;`.z.D;5);`.z.D));0b;()]

b. ?[tab;enlist (>;`date;(-;`.z.D;5));`.z.D));0b;()]

c. ?[tab;enlist (in;`date;2023.03.30 2023.03.31);`.z.D));0b;()]

What I want to do is this

.util.getDateRange wc (where wc can be one of the where clauses from the above functional statements) and I want the output to be

2023.03.26 2023.03.31 for a

or

2023.03.30 2023.03.31 for c

etc.

There are a lot of complexities (ie you have someone typing select from tab where date>2023.03.15).

I thought in order to avoid these complexities I could save the where clause to a variable wc let's say and do something like:

?[([]date:.z.D + neg til .z.D-1950.01.01);wc;();`date]

... but I am ashamed to put such code in prod.

答案1

得分: 1

I think the solution presented is simple and solves the problem. It does return a list of dates rather first/last in the range as in the example, but that could be fixed if required.

If your data is all partitioned by date, then it may be an improvement to use .Q.PV to get the list of partition values on disk, rather than creating some arbitrary date range.

?[([]date:.Q.PV);wc`a;();`date]
英文:

I think the solution presented is simple and solves the problem. It does return a list of dates rather first/last in the range as in the example, but that could be fixed if required.

If your data is all partitioned by date, then it may be an improvement to use .Q.PV to get the list of partition values on disk, rather than creating some arbitrary date range.

?[([]date:.Q.PV);wc`a;();`date]

huangapple
  • 本文由 发表于 2023年4月1日 00:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定