# Functional select with fby, grouping on multiple columns

**URL:** <https://forum.kx.com/t/functional-select-with-fby-grouping-on-multiple-columns/11554>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [June 13, 2017, 7:20pm UTC](https://forum.kx.com/t/functional-select-with-fby-grouping-on-multiple-columns/11554 "2017-06-13T19:20:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alvi\_kabir9191](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@alvi\_kabir9191](https://forum.kx.com/u/alvi_kabir9191)\
**Post date:** [June 13, 2017, 7:20pm UTC](https://forum.kx.com/t/functional-select-with-fby-grouping-on-multiple-columns/11554/1 "2017-06-13T19:20:00Z")

</div>

Hi KDB Gurus,

I have the functional form of this query:

select from rows where 1=(count;i) fby (sym;market;id;price;size;eventType)

as:

?[rows;enlist(=;1;(fby;(enlist;#:;`i);(+:;(!;enlist`sym`market`id`price`size`eventType;(enlist;`sym;`market;`id;`price;`size;`eventType)))));0b;()]

I have my symbol list of columns I want to group on,&nbsp; **`sym`market`id`price`size`eventType,** stored in a local variable **gCols**. I am not sure how to run the functional form using my local variable. Help is much appreciated. Thanks.

---

<div class="post-metadata">

**Author:** ![user9312061](https://avatars.discourse-cdn.com/v4/letter/u/b77776/32.png) [@user9312061](https://forum.kx.com/u/user9312061)\
**Post date:** [January 24, 2024, 2:16pm UTC](https://forum.kx.com/t/functional-select-with-fby-grouping-on-multiple-columns/11554/2 "2024-01-24T14:16:00Z")

</div>

Hey&nbsp;

I know this is an old question, but I guess no harm in having an answer.&nbsp;  
I needed to do this recently and I found myself searching for something online.&nbsp;

Here’s the magic that you need:&nbsp;

assuming I have a variable: .ns.cols:`sym`market`id`price`size`eventType; {[x;y] ?[x;enlist(=;1;(fby;(enlist;#:;`i);(+:; (!;enlist y; (enlist;enlist,y)))));0b;()] }[rows;.ns.cols]

&nbsp;
