4

I have the following kdb table

name   value    price
-------------------------
Paul   1 2      3 4

where value and price are lists. How can I convert them into

name   value    price
------------------------------
Paul   1        3
Paul   2        4

? Thanks!!

Theboy
  • 353
  • 1
  • 2
  • 8

1 Answers1

7

ungroup is what you're looking for here.

As an aside, "value" is a reserved word in q and you should get an 'assign error if you try to use it as a column name.

q)t:([]name:`Paul;value:enlist 1 2;price:enlist 3 4)
'assign
q)t:([]name:`Paul;val:enlist 1 2;price:enlist 3 4)
q)ungroup t
name val price
--------------
Paul 1   3
Paul 2   4
  • 2
    Yes. You can also use .Q.id to rename invalid column names but obviously better to not use the bad name in the first place: ```.Q.id flip `name`value`price!(1#`Paul;enlist 1 2;enlist 3 4)``` – terrylynch May 02 '18 at 13:07
  • Thanks!! What if I have null element? for example, some element in val is actually () – Theboy May 04 '18 at 19:49
  • It will still work in either case but, what you have there is an empty list. A null in q has a type read more here:http://code.kx.com/q4m3/2_Basic_Data_Types_Atoms/#27-nulls – Dónal Ó Loingsigh May 07 '18 at 01:35