kdb - 将 "Sum"行添加到表中

标签 kdb

我有一个查询,该查询创建一个表,按部门(上衣、下衣、珠宝等)拆分销售数据。然后,我设置一个日期期间,并将每个部门的总销售额、商品和成本汇总为单独的列。

我现在想在表格中添加“总计”行。我认为这可以通过使用 insert 来处理:

`SalesByDept insert (enlist `total;sum(QTY);sum(Sales);sum(Cost))

这不起作用,但奇怪的是我什至不能通过这样做插入总行

`SalesByDept insert (enlist `total;1;1;1)

Error: 'type

有人知道我在哪里被绊倒吗?

最佳答案

这里的类型错误告诉您尝试插入的值之一与其要插入的列不相符。

我想它是cost列,它可能是表中的float。无论哪种方式,您都可以通过如下方式找出错误的列:

q)t:([]dep:();qty:();sales:();cost:()); `t insert (10?`1;10?10;10?1000;10?1000.);
q)t2:select sum qty, sum sales, sum cost by dep from t; newrow:(`total;1;1;1)
q)// what columns are matching
q)exec c where type'[newrow]<>neg .Q.t?t from meta t2
,`cost
q)// what's the diffs
q)// t2 cost type
q)meta[t2][`cost;`t]
"f"
q)// newrow cost type
q).Q.t abs type newrow cols[t2]?`cost
"j"
q)// change to expected type (leading dot makes it a float type) and see if insert works
q)newrow:(`total;1;1;1.)
q)// works now
q)`t2 insert newrow
,8

插入“总计”行的另一种方法,无需担心类型:

`t2 upsert (enlist[`dep]!enlist `total),last sums t2

肖恩,HTH

关于kdb - 将 "Sum"行添加到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51574804/

相关文章:

kdb - kdb 中表之间的矩阵乘法

kdb q - 有效地计算平面文件中的表

KDB+ 合并多个更新语句

kdb - 仅将公式应用于当前行和前一行 (Q/KDB)

kdb - kdb中的 'and'或 '&'有什么区别?

KDB - 运行一个函数,其中函数名称和参数作为字符串传入

java - 是否有一个好的算法来检查指定时间段内数据的变化?

KDB\Q : How to run an iterative union join from within a ticker function?

kdb - 从 KDB 中包含 JSON 文本的列中提取多列