2

I have a query that creates a table splitting out sales data by department (tops, bottoms, jewelry, etc). I then set a date period and sum the total sales, items and costs for each department as individual columns.

I'd now like to include a "Total" row to the table. I thought this could be handled by using insert:

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

This doesn't work but the strange thing is I can't even just insert a total row by doing

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

Error: 'type

Anybody know where I'm getting tripped up?

1 Answer 1

4

This type error here is telling you one of the values you are trying to insert is not conforming with the column it is being inserted into.

I imagine it's the cost column, it's likely a float in the table. Either way you can find out the bad column[s] by something like this:

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

An alternative way to insert a 'totals' row, without having to worry about type:

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

HTH, Sean

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I'm still digesting your top part but the bottom upsert definitely takes care of what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.