cancel
Showing results for 
Search instead for 
Did you mean: 

Convert Tree into Table columns

KDB_Dev
New Contributor
Hi All,

I have a problem statement where I need to convert Tree kind of rows into columns.
 
For Ex:-

I have a table Tree where I have rows as follows :

Rule       | Key         | Value
Rule1      | Key1      | Val1
Rule1      | Key2      | Val2
Rule2      | Key1      | Val3
Rule2      | Key2      | Val4
Rule3      | Key1      | Val5
Rule3      | Key2      | Val6
Rule3      | Key3       | Val7

Now I want to convert the unique keys as a column to my output tree and values be the row value.

Output Table :-

Rule       | Key1         | Key2         | Key3
Rule1     | Val1          | Val2          | 
Rule2     | Val3          | Val4          | 
Rule3     | Val5          | Val6          |  Val7


Basically, I want to group all rules together, create columns out of all unique keys and respective values.

Not sure how to do it in kdb. Can somebody help here?

2 REPLIES 2

ajay
New Contributor II


exec distinct[t`Key]#Key!Val by rule:rule from t



KDB_Dev
New Contributor
I got the answer post posting this... We can use pivot.. Thanks to KDB...