执行 KDB select 时获取默认值而不是空值
Get Default value instead of null while doing KDB select
我想在特定列值为 null 或空白时获取默认值。
e.g select customer,date,product,orderId,version,size from tableA where date=2020.04.08,product in (`Derivative)
returns :
+----------+----------+------------+---------+---------+------+
| customer | date | product | orderId | version | size |
+----------+----------+------------+---------+---------+------+
| XYZ fund | 4/8/2020 | Derivative | 1 | 6 | |
| XYZ fund | 4/8/2020 | Derivative | 2 | 6 | 1000 |
| XYZ fund | 4/8/2020 | Derivative | 3 | 4 | |
+----------+----------+------------+---------+---------+------+
所以我想要 NA 而不是下面的空白或 null :
+----------+----------+------------+---------+---------+------+
| customer | date | product | orderId | version | size |
+----------+----------+------------+---------+---------+------+
| XYZ fund | 4/8/2020 | Derivative | 1 | 6 | NA |
| XYZ fund | 4/8/2020 | Derivative | 2 | 6 | 1000 |
| XYZ fund | 4/8/2020 | Derivative | 3 | 4 | NA |
+----------+----------+------------+---------+---------+------+
这很难用像 NA
这样的值来做,因为 q 更喜欢向量是统一类型。假设您的 size
列是数字类型而不是符号,您将 运行 遇到问题。
我建议使用 ^ fill 运算符填写一些数值。例如要填零作为默认值:
q)res:select customer,date,product,orderId,version,size from tableA where date=2020.04.08,product in (`Derivative)
q)update 0^size from res
customer date product orderId version size
-------------------------------------------------
XYZ fund 4/8/2020 Derivative 1 6 0
XYZ fund 4/8/2020 Derivative 2 6 1000
XYZ fund 4/8/2020 Derivative 3 4 0
如果使用 NA
值很重要,那么您可以将该列转换为符号类型并填充 `NA
:
q)res:select customer,date,product,orderId,version,size from tableA where date=2020.04.08,product in (`Derivative)
q)update `NA^`$string size from res
customer date product orderId version size
-------------------------------------------------
XYZ fund 4/8/2020 Derivative 1 6 NA
XYZ fund 4/8/2020 Derivative 2 6 1000
XYZ fund 4/8/2020 Derivative 3 4 NA
这样做的缺点是,如果不先转换回数字类型,您将无法再对列进行数字运算:
q)select sum size from res
'type
[0] select sum size from res
q)select sum "J"$string size from res
size
----
1000
我想在特定列值为 null 或空白时获取默认值。
e.g select customer,date,product,orderId,version,size from tableA where date=2020.04.08,product in (`Derivative)
returns :
+----------+----------+------------+---------+---------+------+
| customer | date | product | orderId | version | size |
+----------+----------+------------+---------+---------+------+
| XYZ fund | 4/8/2020 | Derivative | 1 | 6 | |
| XYZ fund | 4/8/2020 | Derivative | 2 | 6 | 1000 |
| XYZ fund | 4/8/2020 | Derivative | 3 | 4 | |
+----------+----------+------------+---------+---------+------+
所以我想要 NA 而不是下面的空白或 null :
+----------+----------+------------+---------+---------+------+
| customer | date | product | orderId | version | size |
+----------+----------+------------+---------+---------+------+
| XYZ fund | 4/8/2020 | Derivative | 1 | 6 | NA |
| XYZ fund | 4/8/2020 | Derivative | 2 | 6 | 1000 |
| XYZ fund | 4/8/2020 | Derivative | 3 | 4 | NA |
+----------+----------+------------+---------+---------+------+
这很难用像 NA
这样的值来做,因为 q 更喜欢向量是统一类型。假设您的 size
列是数字类型而不是符号,您将 运行 遇到问题。
我建议使用 ^ fill 运算符填写一些数值。例如要填零作为默认值:
q)res:select customer,date,product,orderId,version,size from tableA where date=2020.04.08,product in (`Derivative)
q)update 0^size from res
customer date product orderId version size
-------------------------------------------------
XYZ fund 4/8/2020 Derivative 1 6 0
XYZ fund 4/8/2020 Derivative 2 6 1000
XYZ fund 4/8/2020 Derivative 3 4 0
如果使用 NA
值很重要,那么您可以将该列转换为符号类型并填充 `NA
:
q)res:select customer,date,product,orderId,version,size from tableA where date=2020.04.08,product in (`Derivative)
q)update `NA^`$string size from res
customer date product orderId version size
-------------------------------------------------
XYZ fund 4/8/2020 Derivative 1 6 NA
XYZ fund 4/8/2020 Derivative 2 6 1000
XYZ fund 4/8/2020 Derivative 3 4 NA
这样做的缺点是,如果不先转换回数字类型,您将无法再对列进行数字运算:
q)select sum size from res
'type
[0] select sum size from res
q)select sum "J"$string size from res
size
----
1000