cancel
Showing results for 
Search instead for 
Did you mean: 

flatten out keys to map to values in kdb

Sam1
New Contributor


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?


8 REPLIES 8

Sam1
New Contributor
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



abcfoots
New Contributor

q)g:((`Keys;`Values);(`A`B;`V1);(`C`D`E;`V2))
q)(!). flip raze(,'/).'g,\:\:()
Keys| Values
A   | V1
B   | V1
C   | V2
D   | V2
E   | V2

Sam1
New Contributor
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

kuentang
New Contributor

 

Easy.

 

ungroup flip `Keys`Values!flip((`A`B;`V1);(`C`D`E;`V2))

 

Kim

 

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

 

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

Sam1
New Contributor
here is another challenge.
what is values are also composite

g:([]k:((`A`B);(`C`D`E));v:((`V1`V2);(`V3`V4)))
k      v
------------
`A`B   V1 V2
`C`D`E V3 V4

ungroup doesn't work

Sam1
New Contributor
The result should be
k      v
------------
`A   V1 V2
`B   V1 V2
`C   V3 V4
`D   V3 V4
`E   V3 V4

jim1
New Contributor

([]k:raze g`k;v:raze (count each g`k)#'enlist each g`v)

rspa9428
New Contributor
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 g`k;v:raze (count each g`k)#'enlist each g`v)