Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KDB: String comparison with a table

Tags:

kdb+

I have a table bb:

bb:([]key1: 0 1 2 1 7; col1: 1 2 3 4 5; col2: 5 4 3 2 1; col3:("11";"22" ;"33" ;"44"; "55"))

How do I do a relational comparison of string? Say I want to get records with col3 less than or equal to "33"

select from bb where col3 <= "33"

Expected result:

key1    col1    col2    col3
0       1       5       11
1       2       4       22
2       3       3       33
like image 587
pom Avatar asked Nov 20 '25 11:11

pom


2 Answers

If you want col3 to remain of string type, then just cast temporarily within the qsql query?

q)select from bb where ("J"$col3) <= 33
key1 col1 col2 col3
-------------------
0    1    5    "11"
1    2    4    "22"
2    3    3    "33"
like image 54
jomahony Avatar answered Nov 22 '25 05:11

jomahony


If you are looking for classical string comparison, regardless to if string is number or not, I would propose the next approach:

a. Create methods which behave similar to common Java Comparators. Which returns 0 when strings are equal, -1 when first string is less than second one, and 1 when first is greater than the second

 .utils.compare: {$[x~y;0;$[x~first asc (x;y);-1;1]]};
 .utils.less: {-1=.utils.compare[x;y]};
 .utils.lessOrEq: {0>=.utils.compare[x;y]};
 .utils.greater: {1=.utils.compare[x;y]};
 .utils.greaterOrEq: {0<=.utils.compare[x;y]};

b. Use them in where clause

bb:([]key1: 0 1 2 1 7; 
    col1: 1 2 3 4 5; 
    col2: 5 4 3 2 1; 
    col3:("11";"22" ;"33" ;"44"; "55"));
select from bb where .utils.greaterOrEq["33"]'[col3]

c. As you see below, this works for arbitrary strings

cc:([]key1: 0 1 2 1 7; 
    col1: 1 2 3 4 5; 
    col2: 5 4 3 2 1; 
    col3:("abc" ;"def" ;"tyu"; "55poi"; "gab"));
select from cc where .utils.greaterOrEq["ffff"]'[col3]

.utils.compare could also be written in vector form, though, I'm not sure if it will be more time/memory efficient

.utils.compareVector: {
    ?[x~'y;0;?[x~'first each asc each(enlist each x),'enlist each y;-1;1]]
 };
like image 35
Anton Dovzhenko Avatar answered Nov 22 '25 04:11

Anton Dovzhenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!