我有一个查询,该查询创建一个表,按部门(上衣、下衣、珠宝等)拆分销售数据。然后,我设置一个日期期间,并将每个部门的总销售额、商品和成本汇总为单独的列。
我现在想在表格中添加“总计”行。我认为这可以通过使用 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/