user236215
user236215

Reputation: 7556

how to use ungroup with multiple composite columns in kdb

Consider the following table:

`Keys  `Values
`A`B   `V1`V2
`C`D`E   `V1`V2`V3`V4

I want to flatten the Keys column only such that each key is mapped to the corresponding Values. The result should be:

`Keys  `Values
`A     `V1`V2
`B     `V1`V2
`C     `V1`V2`V3`V4
`D     `V1`V2`V3`V4
`E     `V1`V2`V3`V4

ungroup function applies ungroup to all columns, in this case I want to apply to Keys column alone.

Upvotes: 1

Views: 1604

Answers (1)

MdSalih
MdSalih

Reputation: 1996

Few different ways you can go about this, below is one way -

ungroupCol:{[tbl;col]
    @[tbl where count each tbl col;col;:;raze tbl col]
}

Then:

q)t:flip `Keys`Values!((`A`B;`C`D`E);(`V1`V2;`V1`V2`V3`V4))
q)ungroupCol[t;`Keys]
    Keys Values
    -----------------
    A    `V1`V2
    B    `V1`V2
    C    `V1`V2`V3`V4
    D    `V1`V2`V3`V4
    E    `V1`V2`V3`V4

Upvotes: 3

Related Questions