# where \[condition relating to another table\]

**URL:** <https://forum.kx.com/t/where-condition-relating-to-another-table/8160>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [February 20, 2013, 10:56pm UTC](https://forum.kx.com/t/where-condition-relating-to-another-table/8160 "2013-02-20T22:56:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack31](https://avatars.discourse-cdn.com/v4/letter/j/58956e/32.png) [@jack31](https://forum.kx.com/u/jack31)\
**Post date:** [February 20, 2013, 10:56pm UTC](https://forum.kx.com/t/where-condition-relating-to-another-table/8160/1 "2013-02-20T22:56:00Z")

</div>

hi guys,

i want to do this:  
select from inp where p=first exec p from u where u=`m but i have to do this; select k,v from inp where {inp[`p]=first exec p from u where u=`m}

is there a better way?

q)meta inp

| c | t f a |
| --- | --- |
| u | s |
| p | s p |
| k | s |
| v | |
| q)meta p | |
| c | t f a |
| - | ----- |
| p | s |
| s | C |
| q)select from inp where p=first exec p from u where u=`m | |
| 'type | |
| q)select k,v from inp where {inp[`p]=first exec p from u where u=`m} | |
| k v | |

* * *

price 0.04 0.2 0.002 0.1  
vol 100 120

---

<div class="post-metadata">

**Author:** ![Attila](https://avatars.discourse-cdn.com/v4/letter/a/b9bd4f/32.png) [@Attila](https://forum.kx.com/u/Attila)\
**Post date:** [February 21, 2013, 12:09am UTC](https://forum.kx.com/t/where-condition-relating-to-another-table/8160/2 "2013-02-21T00:09:00Z")

</div>

To: personal-kdbplus@googlegroups.comX-Mailer: Apple Mail (2.1499)name resolution in nested select/exec gets this confused as the second table and a column inside the first table are both being called ut(he where clause on the second table does not matter in this case)q)select x from inp where p in exec p from 0N!u`m`m`j'typea possible workaround is the way you demonstrated - a functionyou could call your second table something elseq)inp:([]u:`m`m`j;p:`p1`p2`p1;x:0 1 2);t:([u:`m`j]p:`p1`p1)q)select x from inp where p in exec p from t where u=`mx-02or the column in the first tableq)inp:(v:`m`m`j;p:`p1`p2`p1;x:0 1 2);t:([u:`m`j]p:`p1`p1)q)select x from inp where p in exec p from u where u=`mx-02name resolution is:column-namelocalglobaleasy to confirm withq)x:`gq){exec count[i]#x from(enlist`c)}`p,`cq){exec count[i]#x from([]y:enlist`c)}`p,`pq){exec count[i]#x from(y:enlist`c)}`p,`gCheers, AttilaOn 20 Feb 2013, at 22:56, Jack Andrews <jack> wrote:&gt; hi guys,&gt; &gt; i want to do this:&gt; select from inp where p=first exec p from u where u=`m\> but i have to do this;\> select k,v from inp where {inp[`p]=first exec p from u where u=`m}\> \> is there a better way?\> \> q)meta inp\> c| t f a\> -| -----\> u| s\> p| s p\> k| s\> v|\> q)meta p\> c| t f a\> -| -----\> p| s\> s| C\> q)select from inp where p=first exec p from u where u=`m&gt; 'type&gt; q)select k,v from inp where {inp[`p]=first exec p from u where u=`m}\> k v\> ----------------------------\> price 0.04 0.2 0.002 0.1\> vol 100 120\> \> – \>

Submitted via Google Groups
