AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题

问题[kdb+](coding)

Martin Hope
Maurice Lim
Asked: 2025-04-20 20:15:20 +0800 CST

Kdb 混合列表类型

  • 6

我偶然发现了一个有趣的错误/功能.Q.ty。

从文档(https://code.kx.com/q/ref/dotq/#ty-type)中可以看出,统一类型列表采用大写字母表示。

但是,我遇到了以下错误/功能:

q).Q.ty(10 10;`abn)
"J"
q).Q.ty 10#(10 10;`abn)
"J"
q)type each(10 10;`abn)
7 -11h
q)type each 10#(10 10;`abn)
7 -11 7 -11 7 -11 7 -11 7 -11h
q)show t:([]a:3 4 5;b:"abc";c:(3;"xy";`ab);d:3 2#3 4 5;e:("abc";"de";"fg");f:(10 10;`abn;10 10))
a b c    d   e     f
------------------------
3 a 3    3 4 "abc" 10 10
4 b "xy" 5 3 "de"  `abn
5 c `ab  4 5 "fg"  10 10
q)meta t
c| t f a
-| -----
a| j
b| c
c|
d| J
e| C
f| J

为什么类型不是混合列表?

kdb+
  • 1 个回答
  • 83 Views
Martin Hope
Utsav
Asked: 2025-04-20 02:26:49 +0800 CST

根据股票代码创建一个具有价格范围的 kdb 表

  • 5

在创建示例测试表时,我尝试生成marketPrice基于securityId列的列值,即

  • securityId=a市场价格可以在 0-100 之间。
  • securityId=b市场价格可能在 100-200 之间。
  • securityId=c市场价格可能在 1000-1100 之间。

我可以想出以下解决方案。您能否分享一个更好的解决方案,让我们可以不用使用任何更新语句,或者最多只用一个带有向量条件或字典之类的更新语句?

n:100;
tradeDate: asc n?2025.04.01 + til 10;
securityId: `g#n?`a`b`c;
quantityAvailable: n?1000;
marketPrice: n?100.;

data:([]
    tradeDate: tradeDate;
    securityId: securityId;
    quantityAvailable: quantityAvailable;
    marketPrice:marketPrice
 );

update marketPrice: 100+marketPrice from `data where securityId=`b;
update marketPrice: 1000+marketPrice from `data where securityId=`c;
kdb+
  • 2 个回答
  • 68 Views
Martin Hope
Utsav
Asked: 2025-04-05 23:18:09 +0800 CST

返回第一个缺失的正整数

  • 6

这里有一个包含正值和负值的整数列表。我们需要返回第一个缺失的正整数。

例如。

  1. 输入 - 3 -1 1 0 4,预期输出 - 2
  2. 输入 - -1 -4 -3,预期输出 - 1
  3. 输入 - 1 3 2,预期输出 - 4

我可以想出下面的解决方案,它可能有很大的优化空间。只是想检查是否有不使用排序逻辑的 O(n) 解决方案。如果没有,那么请采用下面的其他优化解决方案。

q)f: {if[all x<1;:1]; x: asc x where x>0;  b:1+first where not x=1+til count x; $[0N=b;1+last x;b]}
q)f -1 -4 -3
1
q)f 1 3 2
4
q)f 3 -1 1 0 4
2
kdb+
  • 2 个回答
  • 73 Views
Martin Hope
Patryk Maryn
Asked: 2025-04-03 14:42:50 +0800 CST

KDB+ q - 函数中以列表形式返回的值背后的逻辑是什么

  • 5

我有一个简单的函数,然后将其定义为一个列表并作为列表调用:

f:{x*x}
q)(f; neg)[1; 2]
-2
q)(f; neg)[1; 3]
-3
q)(f; neg)[2; 3]
3
q)(f; neg)[4; 3]
3

对于 (f; neg)[1; 3] - 它返回最右边的 neg (-3),但是当第一个参数变为 > 1 时它不再返回 neg,例如: (f; neg)[2; 3] -> 返回 3

kdb+
  • 2 个回答
  • 52 Views
Martin Hope
Utsav
Asked: 2025-03-31 00:13:36 +0800 CST

当迭代器作为函数参数传递时

  • 5

我有以下斐波那契函数,它返回预期的输出

q)fib:{{x,sum -2#x}/[{last[x] < 100};x]}    
q)fib 1 1
1 1 2 3 5 8 13 21 34 55 89 144

{last[x] < 100}我们如何用外部函数的参数替换内部函数中的值 100 ?

预期函数调用结构 -

q)fib[1 1;100]
1 1 2 3 5 8 13 21 34 55 89 144 /- expected output
kdb+
  • 2 个回答
  • 56 Views
Martin Hope
threedom
Asked: 2025-03-29 20:51:09 +0800 CST

如何将前 6 条交易记录作为每笔交易的列附加?

  • 5

列:

date of transaction
uid is the trader id
sym is the trade id 

表格子集:

q) select date, uid, sym from tb

date       uid    sym   
------------------------
2011.08.12 171196 537876
2012.09.08 171196 562161
2012.12.28 171196 570391
2014.04.29 171196 599420
2014.04.29 171196 601520
2014.05.11 171196 602286
2014.06.24 171196 605785
2014.07.19 171196 605686
2011.03.15 160872 524982
2011.07.11 168153 536311
2011.07.25 168153 535616
2011.08.25 30746  537340
2011.01.27 122083 523350
2011.03.05 122083 525676
2011.05.06 122083 531523
2011.01.07 181372 521088
2011.02.07 181372 522780
2011.03.02 181372 523984
2011.03.15 181372 524980
2011.03.21 181372 525448
2011.04.09 181372 529164
2011.04.19 181372 527627
2011.04.28 181372 528302
2011.05.16 181372 530337
2011.06.14 181372 532987

目的

我正在尝试获取每个交易的前 6 次uid交易sym。

uid每个 都有多个sym;对于每个 ,我需要它们之前的 6sym个uid。需要返回所有列,而不仅仅是上面看到的子集。

例如,对于此行:

2011.04.19 181372 527627

它将逐行返回+附加列 - 因此它将返回每个先前日期的每个先前信息,sym并uid在新表中的同一行中匹配 - 因此它是原始表,其中包含包含每行先前数据的附加列。

date       uid    sym     prevdate1    prevuid1   prevsym1  prevdate2   prevuid2  etc.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2011.04.19 181372 527627 2011.01.07 181372 521088 2011.03.02 181372 523984 2011.03.15 181372 524980 2011.03.21 181372 525448

如果没有先前的日期或只有 3 个而不是 6 个等等,那么它仍将返回列,但将为空。

我的尝试:

getHHData:{[tb;syms;colnames;numtrades]
dict:exec i by uid from select uid from tb where sym=syms;
tradedate:first exec date from tb where sym=syms;
histdates:?[?[tb;enlist (in;`uid;(raze;(inv;`dict)));(enlist`uid)!enlist`uid;colnames!colnames];();`uid;`date];
histdatesro:raze each value (asc dict),histdates;
ind:til count histdatesro;
preind:{[prevtrades;ind;tradedate;histdatesro]
{[prevtrades;ind;tradedate;histdatesro]
prevtrades+{where y[z] in x}[tradedate;histdatesro;]each ind
}[;ind;tradedate;histdatesro]
}[;ind;tradedate;histdatesro]each neg 1_til numtrades;
uids:exec uid from tb where sym=syms;
histdata:reverse each key asc(value ?[tb;enlist (in;`uid;`uids);`uid;last colnames])!(value dict);
histCols:{[x;data]
{$[x<count y;y[x];0N]}[x] each data}[;histdata] each til numtrades-1;
flip(`sym`uid,`$string[last colnames],/:string 1+til numtrades-1)!(enlist[count[histdata]#syms],enlist[uids],histCols)
}

包装函数:

getHHDataMerged:{[tb;syms;colnames;numtrades]
raze{[tb;sym;colnames;numtrades](lj/){`sym`uid xkey x
}each {[tb;sym;c;numtrades]getHHData[tb;sym;c;numtrades]
}[tb;sym;;numtrades]each colnames
}[tb;;colnames;numtrades]each syms
}

我正在寻找一种更简洁的方法来解决这个问题。

编辑:

q)updCols:{`$string[x],\:string[y]}
q)prevCols:{if[x=0;:`date`uid`sym]; updCols[;x] `prevdate`prevuid`prevsym}
q)7#f/[tb1;1+til 6]
'type
  [0]  7#f/[tb1;1+til 6]
          ^
q)tb1
date       uid    sym   
------------------------
2011.08.12 171196 537876
2012.09.08 171196 562161
2012.12.28 171196 570391
2014.04.29 171196 599420
2014.04.29 171196 601520
2014.05.11 171196 602286
2014.06.24 171196 605785
2014.07.19 171196 605686
2011.03.15 160872 524982
kdb+
  • 2 个回答
  • 66 Views
Martin Hope
niken
Asked: 2025-03-06 13:08:21 +0800 CST

如何将字符串列表连接成一个长字符串 kdb

  • 5

我有一个像这样的字符串列表

q)select id from temp
id                               
---------------------------------
,"_"                             
"01coin"                         
"0chain"                         
"0dog"                           
"0-knowledge-network"            
"0-mee"                          
"0vix-protocol"                  
"0x"                             
"0x0-ai-ai-smart-contract"       
"0x678-landwolf-1933"            
"0xanon"                         
"0xcoco"                         
"0xdao"                          
"0xgasless-2"                    
"0xgen"
...                  

我需要它看起来像这样

"_,01coin,0chain,0dog,0-knowledge-network,0-mee,0vix-protocol,...etc..."                  

在 kdb 中将这样的列表连接成单个字符串的最佳方法是什么?我应该将我的“字符串”转换为“符号”,然后 vs :/ 它们吗?我已经走到这一步了,但我不知道如何将这个“字符串”列表转换为单个“字符串,......”

("_,";"01coin,";"0chain,";"0dog,";"0-knowledge-network,";"0-mee,";"0vix-protocol,"; ...)
kdb+
  • 1 个回答
  • 38 Views
Martin Hope
kka
Asked: 2025-03-04 23:39:16 +0800 CST

带迭代的 kdb 更新函数

  • 5

我尝试创建一个函数,让输出成为下一次迭代的输入。我传入名为 tab 的表,并希望传递每个阈值。

功能如下:

test:{[data;t]
      
       data:update check:t`t3 from data where id within (t`t1;t`t2)
            
      }/[tab;]threshold
        

数据:

tab:([]id:12 130 44 46 456;side:`S`S`B`S`B;entityVal:123 123 456 456 456;eventType:`New`Filled`New`Partial`Partial)
threshold:([]t1:100 10 39 60 80 100 ;t2:200 20 49 70 90 150;t3:`test1`test2`test3`tes4`tes5`test6);

有人能告诉我这个函数出了什么问题吗?它目前给出的是等级:无效等级或价数(参数计数)错误。

kdb+
  • 1 个回答
  • 30 Views
Martin Hope
threedom
Asked: 2025-02-27 20:05:45 +0800 CST

使用解析创建功能查询

  • 5

我在将解析输出转换为功能选择时遇到了问题。我认为你需要将每个输出转换,为enlist

完整问题:

q)dict:exec i by uid from select uid from tb where sym=syms

一:

q)hd:exec date by uid from select date,ht by uid from tb where uid in raze key dict
q)parse"exec date by uid from select date,ht by uid from tb where uid in raze key dict"
?
(?;`tb;,,(in;`uid;(,/;(!:;`dict)));(,`uid)!,`uid;`date`ht!`date`ht)
()
,`uid
,`date

二:

q)ht:key asc(value exec ht by uid from tb where uid in uids)!(value dict)
q)parse"exec ht by uid from tb where uid in uids"
?
`tb
,,(in;`uid;`uids)
,`uid
,`ht
q)

我的尝试:

一(不起作用):

q)?[(?;`tb;enlist enlist (in;`uid;(,/;(!:;`dict)));(enlist `uid)!enlist `uid;`date`ht!`date`ht);();enlist `uid;enlist `date]
'type
  [0]  ?[(?;`tb;enlist enlist (in;`uid;(,/;(!:;`dict)));(enlist `uid)!enlist `uid;`date`ht!`date`ht);();(enlist`date)!enlist`date]

二(工作):

q) ?[`tb;enlist(in;`uid;`uids);`uid;(enlist`ht)!enlist`ht]
kdb+
  • 1 个回答
  • 46 Views
Martin Hope
CleanSock
Asked: 2025-02-25 04:37:30 +0800 CST

KDB 中用于修改 col 值的代码行数更少

  • 6

我有一张如下表:

q)tab:([]time:.z.t;sym:`ax`bx`cx`ds;col1:`a`a`b`b;price:1.4,1.2,1.3,1.4)
q)tab
time         sym col1 price
---------------------------
20:29:37.309 ax  a    1.4
20:29:37.309 bx  a    1.2
20:29:37.309 cx  b    1.3
20:29:37.309 ds  b    1.4
q)

我的目标是将 col1 中的值从 a->b(其中 a 和 b->a(其中 b 和列名相同))更改为 a->b(其中 b 和列名相同)。下面是我的代码,可以完成这项工作,但是有没有更好的方法可以用更少的代码行来实现这一点?

testTab:update col2:count[i]#enlist `b from tab where col1=`a
testTab:update col2:count[i]#enlist `a from testTab where col1=`b

q)testTab
time         sym col1 price col2
--------------------------------
20:29:37.309 ax  a    1.4   b
20:29:37.309 bx  a    1.2   b
20:29:37.309 cx  b    1.3   a
20:29:37.309 ds  b    1.4   a
q)testTab:`time`sym`col2 xcols testTab  //Bring forward col2

q)testTab
time         sym col2 col1 price
--------------------------------
20:29:37.309 ax  b    a    1.4
20:29:37.309 bx  b    a    1.2
20:29:37.309 cx  a    b    1.3
20:29:37.309 ds  a    b    1.4

q)testTab: delete col1 from testTab    //Delete original col1 from the table
q)testTab: `time`sym`col1 xcol testTab  // Rename col2 to col1

q)testTab
time         sym col1 price
---------------------------
20:29:37.309 ax  b    1.4
20:29:37.309 bx  b    1.2
20:29:37.309 cx  a    1.3
20:29:37.309 ds  a    1.4
q)

谢谢!

kdb+
  • 1 个回答
  • 35 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve