# Functions with select statement

**URL:** https://forum.kx.com/t/functions-with-select-statement/5839
**Category:** Community Support
**Tags:** kdb-and-q
**Created:** [November 20, 2008, 3:24pm UTC](https://forum.kx.com/t/functions-with-select-statement/5839 "2008-11-20T15:24:00Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Swee\_Heng](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Swee\_Heng](https://forum.kx.com/u/Swee_Heng)
#### Post date: [November 20, 2008, 3:24pm UTC](https://forum.kx.com/t/functions-with-select-statement/5839/1 "2008-11-20T15:24:00Z")

</div>

Here’s something I find perplexing:The following definition of f works:q)t:(a:til 10;b:10?10f)q)f:{[x;y]select from x where a within y}q)f[t;3 5]a b----------3 6.2542564 3.6673165 9.844223But when [x;y] is omitted, why does it fail with 'rank:q)f:{select from x where a within y}q)f[t;3 5]'rankSwitch around the function arguments and it works even withoutexplicit declaration of [x;y]:q)f:{select from y where a within x}q)f[3 5;t]a b----------3 6.2542564 3.6673165 9.844223Would appreciate it if someone can enlighten me on this rather non-intuitive aspect of function definitions.SH

---

<div class="post-metadata">

### Author: ![Aaron\_Davies](https://avatars.discourse-cdn.com/v4/letter/a/8e7dd6/32.png) [@Aaron\_Davies](https://forum.kx.com/u/Aaron_Davies)
#### Post date: [November 20, 2008, 4:01pm UTC](https://forum.kx.com/t/functions-with-select-statement/5839/2 "2008-11-20T16:01:00Z")

</div>

On Thu, Nov 20, 2008 at 11:24 PM, Swee Heng wrote:\> Here’s something I find perplexing:\>\> The following definition of f works:\> q)t:(a:til 10;b:10?10f)\> q)f:{[x;y]select from x where a within y}\> q)f[t;3 5]\> a b\> ----------\> 3 6.254256\> 4 3.667316\> 5 9.844223\>\> But when [x;y] is omitted, why does it fail with 'rank:\> q)f:{select from x where a within y}\> q)f[t;3 5]\> 'rank\>\> Switch around the function arguments and it works even without\> explicit declaration of [x;y]:\> q)f:{select from y where a within x}\> q)f[3 5;t]\> a b\> ----------\> 3 6.254256\> 4 3.667316\> 5 9.844223\>\> Would appreciate it if someone can enlighten me on this rather non-\> intuitive aspect of function definitions.a classic gotchaq (mis)interprets a y in the where clause as a column name if thereare no other indicators that it’s a parameterq)unshow get{[x;y]select from x where a within y}(0x0ba0a178a20a040005;`x`y;`symbol$();,`;0b;,(within;`a;`y);?;“{[x;y]selectfrom x where a within y}”)q)unshow get{select from x where a within y}(0x0ba0a178a20a040005;,`x;`symbol$();,`;0b;,(within;`a;`y);?;“{selectfrom x where a within y}”)note the differences in element one (args) of the function objectsanother workaround:q){y;select from x where a within y}[t;3 5]a b----------3 6.2542564 3.6673165 9.844223the same is true of y (and z) in the select clauseq)unshow{select a+y from x}[t;3]'rankq){[x;y]select a+y from x}[t;3]a–3456789101112and probably in the by clause too, tho i can’t think of a test off thetop of my head

---

<div class="post-metadata">

### Author: ![Swee\_Heng](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@Swee\_Heng](https://forum.kx.com/u/Swee_Heng)
#### Post date: [November 20, 2008, 4:28pm UTC](https://forum.kx.com/t/functions-with-select-statement/5839/3 "2008-11-20T16:28:00Z")

</div>

Thanks Aaron! You are right about the by clause too.q)t:(a:100?5)q){select count a by a+x from y}[0;t]a| a-| --0| 101| 322| 243| 234| 11q){select count a by a+y from x}[t;0]'rankq){[x;y]select count a by a+y from x}[t;0]a| a-| --0| 101| 322| 243| 234| 11On Nov 21, 12:01?am, “Aaron Davies” \<aaron.dav…\> wrote:\> On Thu, Nov 20, 2008 at 11:24 PM, Swee Heng \<thesweeh…\> wrote:\> \> Here’s something I find perplexing:\>\> \> The following definition of f works:\> \> q)t:(a:til 10;b:10?10f)\> \> q)f:{[x;y]select from x where a within y}\> \> q)f[t;3 5]\> \> a b\> \> ----------\> \> 3 6.254256\> \> 4 3.667316\> \> 5 9.844223\>\> \> But when [x;y] is omitted, why does it fail with 'rank:\> \> q)f:{select from x where a within y}\> \> q)f[t;3 5]\> \> 'rank\>\> \> Switch around the function arguments and it works even without\> \> explicit declaration of [x;y]:\> \> q)f:{select from y where a within x}\> \> q)f[3 5;t]\> \> a b\> \> ----------\> \> 3 6.254256\> \> 4 3.667316\> \> 5 9.844223\>\> \> Would appreciate it if someone can enlighten me on this rather non-\> \> intuitive aspect of function definitions.\>\> a classic gotcha\>\> q (mis)interprets a y in the where clause as a column name if there\> are no other indicators that it’s a parameter\>\> q)unshow get{[x;y]select from x where a within y}\> (0x0ba0a178a20a040005;`x`y;`symbol$();,`;0b;,(within;`a;`y);?;“{[x;y]select\> from x where a within y}”)\> q)unshow get{select from x where a within y}\> (0x0ba0a178a20a040005;,`x;`symbol$();,`;0b;,(within;`a;`y);?;“{select\> from x where a within y}”)\>\> note the differences in element one (args) of the function objects\>\> another workaround:\>\> q){y;select from x where a within y}[t;3 5]\> a b\> ----------\> 3 6.254256\> 4 3.667316\> 5 9.844223\>\> the same is true of y (and z) in the select clause\>\> q)unshow{select a+y from x}[t;3]\> 'rank\> q){[x;y]select a+y from x}[t;3]\> a\> –\> 3\> 4\> 5\> 6\> 7\> 8\> 9\> 10\> 11\> 12\>\> and probably in the by clause too, tho i can’t think of a test off the\> top of my head\</thesweeh…\>\</aaron.dav…\>
