在 kdb 中使用 dbmain.q 对 hdb 中的数据进行排序

sort data in hdb by using dbmain.q in kdb

我正在尝试对 kdb 中的 hdb 中的 1 或 2 列进行排序,但失败了。这是我的代码

fncol[dbdir;`trade;`sym;xasc]; 

调用时出现长度错误。但是如果我使用这个代码

,我不会有长度错误
fncol[dbdir;`trade;`sym;asc];.

然而,这只会对 sym 列本身进行排序。我希望其他列的数据也根据 sym 列进行更改。

此外,我想将parted 属性应用于sym 列。另外,我试过这样排序

fncol[dbdir;`trade;`sym`ptime;xasc];. also failed

是否有任何迹象表明哪个日期因长度错误而失败?其中一个分区可能有问题。

也许如果您尝试将其中一个日期加载到内存中并手动对其进行排序 IE

`sym xasc select from trade where date=last date 

这可能表明是否存在导致问题的特定分区。

仅供参考,如果您对应用 p# 属性感兴趣,您应该在 dbmaint.q 中尝试 setattrcol。不过,我认为需要先对数据进行排序。

如果您不确定 dbmaint.q 会做什么,您应该始终小心。我从 ascxasc 之后工作的事实得知你每次都在使用测试 hdb。

fncol 应该与一元函数一起使用,即 1 个参数。它的用例是修改单个列。您要做的是修改整个 table,因为您希望相对于 sym 列对整个 table 进行排序。正如 Cathal 在您的后续问题中所概述的那样,对每个日期使用 .Q.dpft 是您想要的。

当你 运行 这个 fncol[dbdir;`trade;`sym;xasc]; 你正在保存一个投影来代替每个日期的 sym 列。

fncol[`:.;`trades;`sym;xasc];
select from trades where date = 2014.04.21
'length
  [0]  select from trades where date = 2014.04.21

q)get `:2014.04.21/trades/sym
k){$[$[#x;~`s=-2!(0!.Q.v y)x;0];.Q.ft[@[;*x;`s#]].Q.ord[<:;x]y;y]}[`p#`sym$`A..
// This is the k definition of xasc with the sym column as the first parameter.

q)xasc
k){$[$[#x;~`s=-2!(0!.Q.v y)x;0];.Q.ft[@[;*x;`s#]].Q.ord[<:;x]y;y]}

// Had you needed to fix your hdb, I managed to undo this using value and indexing to the sym col data.

fncol[`:.;`trades;`sym;{(value x)[1]}];

q)select from trades where date = 2014.04.21
date       sym  time                          src price size
------------------------------------------------------------
2014.04.21 AAPL 2014.04.21D08:00:12.155000000 N   25.31 2450
2014.04.21 AAPL 2014.04.21D08:00:42.186000000 N   25.32 289
2014.04.21 AAPL 2014.04.21D08:00:51.764000000 O   25.34 3167

asc 不会破坏 hdb,因为它只接受 1 个参数并仅按升序保存 sym 列而不是 table.