# Not Join

**URL:** <https://forum.kx.com/t/not-join/8932>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [July 11, 2014, 8:25am UTC](https://forum.kx.com/t/not-join/8932 "2014-07-11T08:25:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas\_agarwal1](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vikas\_agarwal1](https://forum.kx.com/u/vikas_agarwal1)\
**Post date:** [July 11, 2014, 8:25am UTC](https://forum.kx.com/t/not-join/8932/1 "2014-07-11T08:25:00Z")

</div>

Hi,

I am trying to find records which are not join in 2 keyed tables. Till now i have reach to the following query.

trade1:([time:`time$()]sym:`symbol$();price:`float$();size:`int$())

trade2:([time:`time$()]sym:`symbol$();price:`float$();size:`int$())

/Add blank col in trade1

update trade1Col:1 from `trade1

/Add blank col in trade2

update trade2Col:1 from `trade2

(select time,sym,price,size,trade1Col,trade2Col from (trade1 lj trade2) where trade2Col=0N)

uj

select time,sym,price,size,trade1Col,trade2Col from (trade2 lj trade1) where trade1Col=0N

Is there any better way to find the records which are mismatch in 2 tables?

---

<div class="post-metadata">

**Author:** ![derekpiar1](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@derekpiar1](https://forum.kx.com/u/derekpiar1)\
**Post date:** [July 11, 2014, 8:33am UTC](https://forum.kx.com/t/not-join/8932/2 "2014-07-11T08:33:00Z")

</div>

Hi Vikas

Have you looked at except?

## q)trade1:([time:09:00 09:01 09:03]sym:`A`B`C;price:1 2 3)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; q)trade2:([time:09:00 09:01 09:04 09:05]sym:`A`A`B`C;price:1 2 3 4)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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;&nbsp; 2&nbsp;&nbsp;&nbsp;  
09:04 B&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;  
09:05 C&nbsp;&nbsp; 4

I hope this helps

Derek Piar  
Junior Financial Software Developer  
AQUAQ Analytics

---

<div class="post-metadata">

**Author:** ![charlie1](https://avatars.discourse-cdn.com/v4/letter/c/ec9cab/32.png) [@charlie1](https://forum.kx.com/u/charlie1)\
**Post date:** [July 11, 2014, 8:42am UTC](https://forum.kx.com/t/not-join/8932/3 "2014-07-11T08:42:00Z")

</div>

this may also be of interest  
http://code.kx.com/wsvn/code/contrib/sagrawal/lcs

http://code.kx.com/wsvn/code/contrib/sagrawal/lcs/readme.txt

---

<div class="post-metadata">

**Author:** ![vikas\_agarwal1](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vikas\_agarwal1](https://forum.kx.com/u/vikas_agarwal1)\
**Post date:** [July 11, 2014, 11:09am UTC](https://forum.kx.com/t/not-join/8932/4 "2014-07-11T11:09:00Z")

</div>

Thanks&nbsp;Derek. But i think it will not work in my case as i have to use join on few fix columns regardless of other values in another table. it also not working with primary keys table.&nbsp;  
Even though i create temp table with join columns only, and then do a except i will not get index but direct values. it will be doable but i think it will take much time.

Thanks Charles for the solution you have provided. I was trying to refactor diffTables for my need as i do not have where condition and sym column. But i am getting Error rank. Even if i use miller.q and try to run following commands it gives same error:

diffTables[trade1;trade2;`a;`time`sym]

lcs[“ABCABA”;“CABBA”;=]

diffTables:{[t1;t2;c]&nbsp;

&nbsp; &nbsp; i1:til count t1

&nbsp; &nbsp; ;i2:til count t2

&nbsp; &nbsp; ;if[0 = (count i1)&(count i2); :(i1;i2)] /if either entry is empty, return &nbsp; &nbsp;

&nbsp; &nbsp; ;a:t1 i1

&nbsp; &nbsp; ;b:t2 i2

&nbsp; &nbsp; ;if[1\<(count c); [a:flip (t1 i1) c;b:flip (t2 i2) c];[a:t1 i1;b:t2 i2]] &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; /;:all t1=t2 fails in next line

&nbsp; &nbsp; ;il:lcs[a;b;{[x;y] all x=y}] /replace with call to lcsopt instead if optimizing for big tables with long common prefix/suffixes

&nbsp; &nbsp; ;:il

&nbsp; &nbsp; ;dela: (til count a) except il[0] //return delta indices, i.e., not a common subsequence &nbsp;in a

&nbsp; &nbsp; ;delb: (til count b) except il[1] //return delta indices, i.e., not a common subsequence &nbsp;in b

&nbsp; &nbsp; ;:(i1 dela; i2 delb) //return the delta indices in original table

&nbsp; }

I shall try to debug lcs function whats the issue.

---

<div class="post-metadata">

**Author:** ![vikas\_agarwal1](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vikas\_agarwal1](https://forum.kx.com/u/vikas_agarwal1)\
**Post date:** [July 11, 2014, 11:27am UTC](https://forum.kx.com/t/not-join/8932/5 "2014-07-11T11:27:00Z")

</div>

My Bad i was trying to run scripts from QStudio and functions were not having ;(terminate) so it was causing the issue. Thanks again.

---

<div class="post-metadata">

**Author:** ![vikas\_agarwal1](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vikas\_agarwal1](https://forum.kx.com/u/vikas_agarwal1)\
**Post date:** [July 17, 2014, 12:16pm UTC](https://forum.kx.com/t/not-join/8932/6 "2014-07-17T12:16:00Z")

</div>

Hi,

diffTables was throwing length exception in case of 2 tables were not of same length. I corrected by change in&nbsp;snakes function, Line no 43. Not sure if i am doing something wrong or it required correction.

;condTrue:1b;

&nbsp; &nbsp; &nbsp; while[(x\<n) and (y\<m) and condTrue;if[f[a;b[y]];condTrue:0b];x+:1;y+:1]; //f is boolean function on a[i],b[j]
