# RE: \[personal kdb+\] Sorting without combining

**URL:** <https://forum.kx.com/t/re-personal-kdb-sorting-without-combining/9821>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [May 13, 2015, 3:37pm UTC](https://forum.kx.com/t/re-personal-kdb-sorting-without-combining/9821 "2015-05-13T15:37:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![david\_demner](https://avatars.discourse-cdn.com/v4/letter/d/7993a0/32.png) [@david\_demner](https://forum.kx.com/u/david_demner)\
**Post date:** [May 13, 2015, 3:37pm UTC](https://forum.kx.com/t/re-personal-kdb-sorting-without-combining/9821/1 "2015-05-13T15:37:00Z")

</div>

User-Agent: Workspace Webmail 5.14.0Message-Id: \<20150513073755.85f80dae80d1d2f2e266ec6278e6cbe8.99bb5f948c.wbe@email07.europe.secureserver.net\>From: “David Demner (AquaQ)” \<david.demner\>To: personal-kdbplus@googlegroups.comSubject: RE: [personal kdb+] Sorting without combiningDate: Wed, 13 May 2015 07:37:55 -0700Mime-Version: 1.0

iasc is the keyword you’re looking for

  

  

t:([]a:11 12t;bid:(1 4 3 2;1 1 3 2);bid\_size:(100 100 200 300;100 200 300 100); bid\_book:2#enlist `CBOE`NYSE`ARCA`AMEX)  

q)delete inds from update bid:bid@'inds, bid\_size:bid\_size@'inds, bid\_book:bid\_book@'inds from update inds:iasc each bid from t

a bid bid\_size bid\_book

--------------------------------------------------------

11:00:00.000 1 2 3 4 100 300 200 100 CBOE AMEX ARCA NYSE

12:00:00.000 1 1 2 3 100 200 100 300 CBOE NYSE AMEX ARCA

  

  

 

>

---

<div class="post-metadata">

**Author:** ![sohagan](https://avatars.discourse-cdn.com/v4/letter/s/6a8cbe/32.png) [@sohagan](https://forum.kx.com/u/sohagan)\
**Post date:** [May 18, 2015, 11:09pm UTC](https://forum.kx.com/t/re-personal-kdb-sorting-without-combining/9821/2 "2015-05-18T23:09:00Z")

</div>

Hi,

Late to the question here but I feel there are a few points worth mentioning…

//define a more realistic table as well as original table

t:(a:11 12t;bid:(1 4 3 2;1 1 3 2);bid\_size:(100 100 200 300;100 200 300 100); bid\_book:2#enlist `CBOE`NYSE`ARCA`AMEX);

n:10000000;

t2:(a:n?`minute$60*0+til 20;bid:n?100;bid_size:n?1000;bid_book:n?`CBOE`NYSE`ARCA`AMEX);

t3:`a xgroup t2;

//some metrics on first query

//im not a fan of nested/cumulative updates - they can get very long winded

//it obviously still works, just not a fan

q)\ts:1000 delete inds from update bid:bid@'inds, bid\_size:bid\_size@'inds, bid\_book:bid\_book@'inds from update inds:iasc each bid from t

5 3712

q)\ts delete inds from update bid:bid@'inds, bid\_size:bid\_size@'inds, bid\_book:bid\_book@'inds from update inds:iasc each bid from t3

1422 335547232

//if table is not grouped originally, then you can do a 2 column sort on it prior to grouping. xasc can be very useful for you here.

q)\ts:1000 `a xgroup `a`bid xasc ungroup t

23 6480

q)\ts `a xgroup `a`bid xasc ungroup t3

3214 1073743312

//alternative - update on top of an exec

q)\ts:1000 ![t;();0b;exec `bid`bid\_size`bid\_book!enlist each (bid;bid\_size;bid\_book)@':iasc each bid from t]

4 2688

q)\ts ![t3;();0b;exec `bid`bid\_size`bid\_book!enlist each (bid;bid\_size;bid\_book)@':iasc each bid from t3]

921 335546608

//you can also consider the use of fby - just for talk sake

q)\ts ![t3;();0b;exec enlist each `bid`bid\_size`bid\_book!(bid;bid\_size;bid\_book)@‘:(iasc’;bid) fby a from t3]

920 335546720

//but if we step back from the problem - it looks like “a” can be your key - if so we can now use the fact that you now make a dict to dict relationship and modify contents of each dict

//this will keep the speed and mem usage down to a minimal

q)\ts:1000 {f,'{flip `bid xasc flip y x}[;x] each f:key x}1!t

26 3008

q)\ts {f,'{flip `bid xasc flip y x}[;x] each f:key x}t3

486 255856784

//confirm match

q)(ungroup {f,'{flip `bid xasc flip y x}[;x] each f:key x}t3)~ungroup delete inds from update bid:bid@'inds, bid\_size:bid\_size@'inds, bid\_book:bid\_book@'inds from update inds:iasc each bid from t3

1b

HTH,

Sean
