Sam11
1
Consider following table in kdb:
`Keys `Values`A`B `V1`C`D`E `V2
I want to flatten out Keys to map to corresponding value.that is, transform to:
`Key `Values`A `V1`B `V1`C `V2`D `V2`E `V2
Is there a succinct and easy way of doing this over looping over each entry using each?
Sam11
2
Alternatively,
is there a way to query part of key?
e.g. in the following table
`Keys `Values`A`B `V1`C`D`E `V2
Lookup for A should return
V1
q)g:((Keys;
Values);(A
B;V1);(
CD
E;`V2))
q)(!). flip raze(,'/).'g,::()
Keys| Values
A | V1
B | V1
C | V2
D | V2
E | V2
Sam11
4
I’ve no idea what you did but it works.
How do you make it work if g is table
`Keys `Values`A`B `V1`C`D`E `V2
<o:p> </o:p>
Easy.<o:p></o:p>
<o:p> </o:p>
ungroup flip Keys
Values!flip((A
B;V1);(
CD
E;`V2))<o:p></o:p>
<o:p> </o:p>
Kim<o:p></o:p>
<o:p> </o:p>
Von: personal-kdbplus@googlegroups.com [mailto:personal-kdbplus@googlegroups.com] Im Auftrag von Sam
Gesendet: Freitag, 2. September 2016 21:13
An: Kdb+ Personal Developers
Betreff: [personal kdb+] Re: flatten out keys to map to values in kdb<o:p></o:p>
<o:p> </o:p>
I’ve no idea what you did but it works.
How do you make it work if g is table <o:p></o:p>
`Keys `Values
`A`B `V1
`C`D`E `V2<o:p></o:p>
Sam11
6
here is another challenge.
what is values are also composite
g:(k:((A
B);(C
DE));v:((
V1V2);(
V3`V4)))
k v
A
B V1 V2
C
D`E V3 V4
ungroup doesn’t work
Sam11
7
The result should be
k v
A V1 V2
B V1 V2
C V3 V4
D V3 V4
`E V3 V4
jim11
8
(k:raze gk;v:raze (count each g
k)#'enlist each g`v)
Nice. Also “where” works nicely instead of "count each… # '…):
flip k
v!(raze k;g[v] where count each k:g
k)
or
select raze k,v:v where count each k from g
Cheers
Ryan
On Sunday, September 4, 2016 at 7:14:22 AM UTC-4, James Little wrote:
(k:raze gk;v:raze (count each g
k)#'enlist each g`v)