将 "Sum" 行添加到 table
Add a "Sum" Row to a table
我有一个创建 table 按部门(上衣、下装、珠宝等)拆分销售数据的查询。然后我设置一个日期期间并将每个部门的总销售额、项目和成本汇总为单独的列。
我现在想在 table 中添加 "Total" 行。我认为这可以通过使用 insert:
来处理
`SalesByDept insert (enlist `total;sum(QTY);sum(Sales);sum(Cost))
这行不通,但奇怪的是我什至不能通过
插入总行
`SalesByDept insert (enlist `total;1;1;1)
Error: 'type
有人知道我在哪里被绊倒了吗?
这里的 type
错误告诉您您尝试插入的值之一与要插入的列不一致。
我想是 cost
列,可能是 table 中的 float
。无论哪种方式,您都可以通过这样的方式找出错误的列[s]:
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
另一种插入 'totals' 行的方法,无需担心类型:
`t2 upsert (enlist[`dep]!enlist `total),last sums t2
HTH,肖恩
我有一个创建 table 按部门(上衣、下装、珠宝等)拆分销售数据的查询。然后我设置一个日期期间并将每个部门的总销售额、项目和成本汇总为单独的列。
我现在想在 table 中添加 "Total" 行。我认为这可以通过使用 insert:
来处理`SalesByDept insert (enlist `total;sum(QTY);sum(Sales);sum(Cost))
这行不通,但奇怪的是我什至不能通过
插入总行`SalesByDept insert (enlist `total;1;1;1)
Error: 'type
有人知道我在哪里被绊倒了吗?
这里的 type
错误告诉您您尝试插入的值之一与要插入的列不一致。
我想是 cost
列,可能是 table 中的 float
。无论哪种方式,您都可以通过这样的方式找出错误的列[s]:
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
另一种插入 'totals' 行的方法,无需担心类型:
`t2 upsert (enlist[`dep]!enlist `total),last sums t2
HTH,肖恩