首页 > 解决方案 > 如何使用 wj 在表中存储时间戳列表

问题描述

我有一个按日期、符号和 client_type 聚合的表。其余的列不是聚合的,而是列表。

例如:

table2: select requestId, initialTimestamp by date, sym, client_type, from table

requestId 和 initialTimestamp 因此是列表。

我想基本上在 5 分钟内找到重复数据并将它们标记为如此。

如果我的 5 分钟是固定的,那么我可以执行以下操作:

table3: update bucket:  (5 xbar\:`minute$initialTimestamp) from table2;

然后取消分组并与存储桶一起工作以识别骗子......例如

table4: select requestId by bucket, sym, client_type ungroup table3;
update duplicateId: ` from (ungroup update duplicateId: ?[(count each requestId)>1; requestId@'1; `] from table4) where requestId=duplicateId

这很好用,但是如果我想要一个 5 分钟的滚动窗口而不是固定窗口怎么办?

它似乎指向 wj - 但我不确定如何使其与分组列一起使用。

标签: kdb

解决方案


如果您只想查找重复项,您可以使用fby而不是时间桶/窗口?

select from table where 1 < (count;initialTimestamp) fby ([]date;sym;client_type;requestId)

编辑:我仍然认为 wj 是不必要的,也不会那么有效。您可以像这样按组进行更新:

// given the following dummy table

t:update time:"P"$"D" sv/: flip(string[date];string[time]) from ([]date:raze 20#'.z.d+til 5;sym:100?`symA`symB`symC;time:10:00+til 100;clientType:100?`clientA`clientB;requestId:100?3);

update dup:00:05>=deltas time by date,sym,clientType,requestId from t

编辑:既然你想要第一个副本,那么我认为这wj可能是你最初想要的唯一方法。您想使用wj1aswj将在进入时间窗口之前考虑当前行。

// sort by grouping then time
t2:`date`sym`clientType`time xasc t; 
times:-00:05 -00:00:00.000000001 +\:t2`time;

// params
// pair of time lists
// common columns/grouping with time last
// tableToJoinTo
// (windowJoinTable;(function;col))

wj1[times;`date`sym`clientType`time;t2;(update dupId:requestId from t2;(first;`dupId))]


推荐阅读