KDB+ Learning Notes (4): Solving ‘type’ Errors

PeterBanng
2 min readOct 19, 2021

A) Update rows regarding given condition but get ‘type’ error

The error is from trying to update value for selected rows that in the type not conform with the type of the rest. Basic datatypes used for conditional calculations are: float, long, short. The corresponding type code is:

q) type each exec from 1?table_1
a | -9
b | -7
c | -5
// a is a float varible column
// b is a long variable column
// c is a short variable column

If updating some of the rows with values of different type, which usually happen when updating selected rows with conditions, there will be ‘type’ error.

q) table_1: update a:1j from table_1 where not null c
`type

B) Return error message null when doing ‘group-by’ calculation with splayed tables

Large data sets saved in the form of spalyed tables can be accessed more efficiently. However, doing direct calculation using group-by with splayed tables can bring error. It is most of the time very confusing as the erorr code is usually the columns in the select code, which are all right with their types.

q) select prob (ColumnA>0) by Category from SplayedTables where [Condition]
`ColumnA

One of the way to fix it is to first get the data from the splayed table and do the calculation with selected data. This is also more neat as we can organize the data we retrieve from saved spalyed tables.

q) df_selected: select Category,ColumnA from SplayedTables where [Condition]
q) select prob (ColumnA>0) by Category from df_selected

Last updated 2021/11/01

--

--