英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论