# how to ungroup only a specific column?

**URL:** <https://forum.kx.com/t/how-to-ungroup-only-a-specific-column/7412>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [November 12, 2011, 6:00pm UTC](https://forum.kx.com/t/how-to-ungroup-only-a-specific-column/7412 "2011-11-12T18:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Felix2](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@Felix2](https://forum.kx.com/u/Felix2)\
**Post date:** [November 12, 2011, 6:00pm UTC](https://forum.kx.com/t/how-to-ungroup-only-a-specific-column/7412/1 "2011-11-12T18:00:00Z")

</div>

hi, gurus

?

it seems that ungroup will un-group all the columns, can we specify that this func only ungroup a specific column?

?

thanks

–  
CHEN, Cheng

---

<div class="post-metadata">

**Author:** ![P\_Bukowinski1](https://avatars.discourse-cdn.com/v4/letter/p/bbe5ce/32.png) [@P\_Bukowinski1](https://forum.kx.com/u/P_Bukowinski1)\
**Post date:** [November 12, 2011, 7:56pm UTC](https://forum.kx.com/t/how-to-ungroup-only-a-specific-column/7412/2 "2011-11-12T19:56:00Z")

</div>

I can propose following solution;  
Function takes one parameter: column name which you don't want to ungroup.

q)exungroup:{(ungroup ![y;();0b;(),x]) lj ?[y;();k!k:key[flip key  
y];((),x)!(),x]}

## q)t:( sym:100?`1;i:100?20;p:100?23.;v:100?100000) q)t sym i p v

e 3 21.92567 8121  
g 13 3.961286 4918  
d 0 12.85666 61617  
g 0 19.59731 5371  
f 10 17.95478 23832  
h 4 21.79956 44355  
g 8 14.8769 25488  
h 0 18.99281 93368  
d 1 20.28972 30654  
b 9 20.809 97596  
j 2 5.604267 87893  
l 10 2.158805 11572  
i 12 21.38842 35546  
f 5 3.028116 68746  
h 17 13.75681 31329  
f 13 17.27875 64688  
f 12 17.93252 80028  
g 6 18.13312 40487  
m 1 10.83011 9660  
g 11 1.694896 89648  
..  
q)select p,v by sym,i from t

| sym x | p v |
| --- | --- |
| a 39 | 12.88096 14288 |
| b 9 | 20.809 97596 |
| b 42 | 14.18556 4206 |
| b 44 | 16.07248 51630 |
| b 51 | 22.05053 77299 |
| b 67 | 12.14168 21926 |
| c 20 | 7.245718 39261 |
| c 21 | 21.41127 95470 |
| c 23 | 1.175395 33229 |
| c 37 | 10.14344 63539 |
| c 52 | 13.47885 60766 |
| c 69 | 16.36479 48806 |
| c 82 | 9.446026 25679 |
| c 86 | 17.95308 60009 |
| c 96 | 19.81956 85916 |
| d 2 | 12.85666 61617 |
| d 8 | 20.28972 30654 |
| d 38 | 19.79639 13355 |
| d 41 | 12.47768 53652 |
| d 43 | 7.264682 28656 |
| .. | |
| q)t:( sym:100?`1;idx:100?20;p:100?23.;v:100?100000) | |
| q)select p,v by sym,idx from t | |
| sym idx | p v |
| ------- | ------------------------------------------ |
| a 5 | ,3.371723 ,71437 |
| a 14 | ,1.896852 ,57948 |
| a 15 | ,20.63499 ,42447 |
| b 11 | ,20.82098 ,39906 |
| b 13 | ,22.27796 ,43307 |
| b 15 | ,1.451067 ,26550 |
| c 3 | ,20.97922 ,2250 |
| c 9 | 17.71302 17.47599 18.22834 9576 37924 7198 |
| c 15 | ,20.40465 ,67581 |
| c 18 | ,15.30607 ,37088 |
| d 3 | 4.392099 2.315363 39281 53319 |
| d 6 | ,21.14576 ,17189 |
| d 12 | 9.985596 15.67237 37494 77233 |
| d 14 | ,5.685739 ,56704 |
| d 17 | 5.019121 14.0118 66773 46627 |
| d 19 | ,21.5716 ,33618 |
| e 2 | ,0.5479222 ,90334 |
| e 3 | 3.03961 12.99523 49452 66488 |
| e 8 | ,13.06477 ,45688 |
| e 9 | 17.59818 13.99597 72586 16359 |
| .. | |
| q)exungroup[`v] select p,v by sym,idx from t | |
| sym idx p v | |

* * *

a 5 3.371723 71437  
a 14 1.896852 57948  
a 15 20.63499 42447  
b 11 20.82098 39906  
b 13 22.27796 43307  
b 15 1.451067 26550  
c 3 20.97922 2250  
c 9 17.71302 9576 37924 7198  
c 9 17.47599 9576 37924 7198  
c 9 18.22834 9576 37924 7198  
c 15 20.40465 67581  
c 18 15.30607 37088  
d 3 4.392099 39281 53319  
d 3 2.315363 39281 53319  
d 6 21.14576 17189  
d 12 9.985596 37494 77233  
d 12 15.67237 37494 77233  
d 14 5.685739 56704  
d 17 5.019121 66773 46627  
d 17 14.0118 66773 46627  
..

## q)exungroup[`v] select p,v,idx by sym from t sym p idx v

a 3.371723 5 71437 42447 57948  
a 20.63499 15 71437 42447 57948  
a 1.896852 14 71437 42447 57948  
b 20.82098 11 39906 43307 26550  
b 22.27796 13 39906 43307 26550  
b 1.451067 15 39906 43307 26550  
c 17.71302 9 9576 2250 67581 37924 37088 7198  
c 20.97922 3 9576 2250 67581 37924 37088 7198  
c 20.40465 15 9576 2250 67581 37924 37088 7198  
c 17.47599 9 9576 2250 67581 37924 37088 7198  
c 15.30607 18 9576 2250 67581 37924 37088 7198  
c 18.22834 9 9576 2250 67581 37924 37088 7198  
d 5.685739 14 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 21.5716 19 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 9.985596 12 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 4.392099 3 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 5.019121 17 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 21.14576 6 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 14.0118 17 56704 33618 37494 39281 66773 17189 46627 77233 53319  
d 15.67237 12 56704 33618 37494 39281 66773 17189 46627 77233 53319  
..  
q)exungroup[`v`idx] select p,v,idx by sym from t  
sym p v idx ..  
-----------------------------------------------------------------------------..  
a 3.371723 71437 42447 57948 5 15 14 ..  
a 20.63499 71437 42447 57948 5 15 14 ..  
a 1.896852 71437 42447 57948 5 15 14 ..  
b 20.82098 39906 43307 26550 11 13 15 ..  
b 22.27796 39906 43307 26550 11 13 15 ..  
b 1.451067 39906 43307 26550 11 13 15 ..  
c 17.71302 9576 2250 67581 37924 37088 7198 9 3 15 9 1..  
c 20.97922 9576 2250 67581 37924 37088 7198 9 3 15 9 1..  
c 20.40465 9576 2250 67581 37924 37088 7198 9 3 15 9 1..  
c 17.47599 9576 2250 67581 37924 37088 7198 9 3 15 9 1..  
c 15.30607 9576 2250 67581 37924 37088 7198 9 3 15 9 1..  
c 18.22834 9576 2250 67581 37924 37088 7198 9 3 15 9 1..  
d 5.685739 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 21.5716 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 9.985596 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 4.392099 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 5.019121 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 21.14576 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 14.0118 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
d 15.67237 56704 33618 37494 39281 66773 17189 46627 77233 53319 14 19 12 3..  
..

Thanks,  
Patryk

2011/11/12 CHEN, Cheng :  
\> hi, gurus  
\>  
\> it seems that ungroup will un-group all the columns, can we specify that  
\> this func only ungroup a specific column?  
\>  
\> thanks  
\>  
\> --  
\> CHEN, Cheng  
\>  
\> --  
\>

Submitted via Google Groups

---

<div class="post-metadata">

**Author:** ![Felix2](https://avatars.discourse-cdn.com/v4/letter/f/6f9a4e/32.png) [@Felix2](https://forum.kx.com/u/Felix2)\
**Post date:** [November 13, 2011, 8:31am UTC](https://forum.kx.com/t/how-to-ungroup-only-a-specific-column/7412/3 "2011-11-13T08:31:00Z")

</div>

thanks a lot!

&nbsp;

let me try ?

---

<div class="post-metadata">

**Author:** ![Rolf1](https://avatars.discourse-cdn.com/v4/letter/r/7c8e57/32.png) [@Rolf1](https://forum.kx.com/u/Rolf1)\
**Post date:** [November 14, 2011, 9:47pm UTC](https://forum.kx.com/t/how-to-ungroup-only-a-specific-column/7412/4 "2011-11-14T21:47:00Z")

</div>

f:{ungroup[![y;();0b;x]]lj key[y]!(x:(),x)#0!y}
