# help with sample query on code.kx.com

**URL:** <https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [August 30, 2016, 7:49am UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219 "2016-08-30T07:49:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hungry\_Goat1](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@Hungry\_Goat1](https://forum.kx.com/u/Hungry_Goat1)\
**Post date:** [August 30, 2016, 7:49am UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/1 "2016-08-30T07:49:00Z")

</div>

On this page ([http://code.kx.com/wiki/Sample\_Queries](http://code.kx.com/wiki/Sample%5C_Queries)), there is a sample q code

```
<font color='"#000000"'>/ Generate some random computer statistics (cpu usage only)

```

/ You can modify n (number of unique computers), timerange (how long the data is for)  
/ freq (how often a computer publishes a statistic) and calls (the number of logged calls)  
n:1000; timerange:5D; freq:0D00:01; calls:3000;  
depts:`finance`packing`logistics`management`hoopjumping`trading`telesales; startcpu:(til n)!25+n?20; fcn:n*fc:`long$timerange%freq;

computer:(time:(-0D00:00:10 + fcn?0D00:00:20)+fcn#(.z.p - timerange)+freq\*til fc; id:raze fc#'key startcpu)  
computer:update `g#id from `time xasc update cpu:{100&3|startcpu[first x]+sums(count x)?-2 -1 -1 0 0 1 1 2}[id] by id from computer

/ And generate some random logged calls  
calls:( time:(.z.p - timerange)+asc calls?timerange; id:calls?key startcpu; severity:calls?1 2 3)

/ create a lookup table of computer information  
computerlookup:([id:key startcpu] dept:n?depts; os:n?`win7`win8`osx`vista)

The line in red above gives me an error

q)\l sample.q

k){$[$[99h=@t:v y;98h=@. t;0];[n:#+!y;n!x 0!y];x y]}

's-fail

@

@[;`time;#[`s]]

+`time`id`cpu!(2016.08.25D06:45:04.189595562 2016.08.25D06:45:04.226940205 20..

q.Q))

The error seems to be sorting the computer table by time. I can’t seem to figure out why the sorting is failing or how to fix it.

---

<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:** [August 30, 2016, 8:49am UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/2 "2016-08-30T08:49:00Z")

</div>

which version/release of kdb+ are you using?

---

<div class="post-metadata">

**Author:** ![Hungry\_Goat1](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@Hungry\_Goat1](https://forum.kx.com/u/Hungry_Goat1)\
**Post date:** [August 30, 2016, 12:02pm UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/3 "2016-08-30T12:02:00Z")

</div>

KDB version on which it is NOT working (This is the paid 64-bit version running on Linux):

KDB+ 3.1 2013.07.18 Copyright (C) 1993-2013 Kx Systems

l64/ 1(1)core

KDB version on which it is working (This is the free 32 bit version running on Windows):

KDB+ 3.1 2014.08.22 Copyright (C) 1993-2014 Kx Systems

w32/ 16()core

---

<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:** [August 30, 2016, 12:14pm UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/4 "2016-08-30T12:14:00Z")

</div>

yes, that bug was fixed in v3.1 2013.10.06

---

<div class="post-metadata">

**Author:** ![Hungry\_Goat1](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@Hungry\_Goat1](https://forum.kx.com/u/Hungry_Goat1)\
**Post date:** [August 30, 2016, 4:51pm UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/5 "2016-08-30T16:51:00Z")

</div>

Thanks Charles. Appreciate if you can share more details of the bug.

---

<div class="post-metadata">

**Author:** ![Hungry\_Goat1](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@Hungry\_Goat1](https://forum.kx.com/u/Hungry_Goat1)\
**Post date:** [August 30, 2016, 4:57pm UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/6 "2016-08-30T16:57:00Z")

</div>

On Tuesday, August 30, 2016 at 3:01:13 PM UTC+8, Hungry Goat wrote:

> On this page (http://code.kx.com/wiki/Sample\_Queries), there is a sample q code
> 
> / Generate some random computer statistics (cpu usage only)/ You can modify n (number of unique computers), timerange (how long the data is for)/ freq (how often a computer publishes a statistic) and calls (the number of logged calls)n:1000; timerange:5D; freq:0D00:01; calls:3000;depts:`finance`packing`<wbr>logistics`management`<wbr>hoopjumping`trading`telesales; startcpu:(til n)!25+n?20; fcn:n*fc:`long$timerange%freq;computer:(time:(-0D00:00:10 + fcn?0D00:00:20)+fcn#(.z.p - timerange)+freq\*til fc; id:raze fc#'key startcpu)computer:update `g#id from `time xasc update cpu:{100&3|startcpu[first x]+sums(count x)?-2 -1 -1 0 0 1 1 2}[id] by id from computer/ And generate some random logged callscalls:( time:(.z.p - timerange)+asc calls?timerange; id:calls?key startcpu; severity:calls?1 2 3)/ create a lookup table of computer informationcomputerlookup:([id:key startcpu] dept:n?depts; os:n?`win7`win8`osx`vista)
> 
> The line in red above gives me an error
> 
> q)\l sample.q
> 
> k){$[$[99h=@t:v y;98h=@. t;0];[n:#+!y;n!x 0!y];x y]}
> 
> 's-fail
> 
> @
> 
> @[;`time;#[`s]]
> 
> +`time`id`cpu!(2016.08.25D06:45:04.189595562 2016.08.25D06:45:04.226940205 20..
> 
> q.Q))
> 
> The error seems to be sorting the computer table by time. I can’t seem to figure out why the sorting is failing or how to fix it.

---

<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:** [August 30, 2016, 5:11pm UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/7 "2016-08-30T17:11:00Z")

</div>

As a commercially licensed user, you have access to the detailed change list via your company’s representative (they can download new versions/releases AND the detailed change list).

In addition, as a commercially licensed user, you’re also welcome to join the k4 listbox by submitting your company email address ( **_not_** your personal gmail/hotmail etc) here&nbsp;https://www.listbox.com/subscribe/?listname=k4

However, the relevant change was  
sorting of vectors of underlying type long was broken where range of values\<9e15 with values\>9e15.  
&nbsp;e.g. q)n:10;b:(100\*a:n?n)+50\*floor n#1e17;iasc[b]~iasc a

---

<div class="post-metadata">

**Author:** ![Hungry\_Goat1](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@Hungry\_Goat1](https://forum.kx.com/u/Hungry_Goat1)\
**Post date:** [August 30, 2016, 5:22pm UTC](https://forum.kx.com/t/help-with-sample-query-on-code-kx-com/11219/8 "2016-08-30T17:22:00Z")

</div>

Thank you Charles. Appreciate your prompt response.
