Skip to content

ungroup

ungroup x    ungroup[x]

Where x is a table, in which some cells are lists, but for any row, all lists are of the same length, returns the normalized table, with one row for each item of a lists.

q)p:((enlist 2);5 7 11;13 17)
q)r:((enlist"A");"CDE";"FG")

q)show t:([]s:`a`b`c;p;q:10 20 30;r)
s p      q  r
-----------------
a ,2     10 ,"A"
b 5 7 11 20 "CDE"
c 13 17  30 "FG"

q)ungroup t             / flatten lists p and r
s p  q  r
---------
a 2  10 A
b 5  20 C
b 7  20 D
b 11 20 E
c 13 30 F
c 17 30 G

Typically used on the result of xgroup or select.

q)\l sp.q
q)show t:select p,qty by s from sp where qty>200
s | p            qty
--| ------------------------
s1| `p$`p1`p3`p5 300 400 400
s2| `p$`p1`p2    300 400
s4| `p$,`p4      ,300

q)ungroup t
s  p  qty
---------
s1 p1 300
s1 p3 400
s1 p5 400
s2 p1 300
s2 p2 400
s4 p4 300

ungroup is not the exact inverse of grouping

Grouping sorts on the keys, so a subsequent ungroup returns the original records sorted by the grouped column/s.


group, select, xgroup
Q for Mortals ยง9.3.4.2 Grouping without Aggregation