# Sort a column for all dates for a partitoned table

**URL:** <https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [April 17, 2018, 6:52pm UTC](https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742 "2018-04-17T18:52:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bajajank](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bajajank](https://forum.kx.com/u/bajajank)\
**Post date:** [April 17, 2018, 6:52pm UTC](https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742/1 "2018-04-17T18:52:00Z")

</div>

Hi all, I have a partitoned table with data worth more than a year and it has a time column. I notice that the time range query like below are very slow or times out \> 5 sec. select from trade where date = 2017.08.03, time within (08:00:00;08:01:00) meta trade c t f a date d sym s p time n …and few more columns I read online that setting sort attribute might help in such cases. So I tried to set the sort attribute using dbmaint.q but got the below error. setattrcol[`:.;`trade;`time;`s] ERROR:s-fail s-fail invalid attempt to set sorted attribute I think the above error is because the time column is not sorted and I need to sort the time column for all the dates so that the sort attribute can be applied to the time column. Is there a way to sort the time column for all the dates instead of manually sorting each date? Secondly, going forward, is there any way to ensure the time column is always sorted in realtime tick data as well as hostorical? Is there a way to force sorting on a particular column on a save down? Appreciate any help with resolving this!!! Thanks, Ankur

---

<div class="post-metadata">

**Author:** ![Flying1](https://avatars.discourse-cdn.com/v4/letter/f/f4b2a3/32.png) [@Flying1](https://forum.kx.com/u/Flying1)\
**Post date:** [April 18, 2018, 5:48am UTC](https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742/2 "2018-04-18T05:48:00Z")

</div>

Sorting can be done whenever a table is updated/saved down. It’s a matter of changing the upd or related functions to add in the sorting step. Alternatively, for hdb, you can choose to sort the on-disk data after it’s been saved down.

---

<div class="post-metadata">

**Author:** ![James\_Burrows](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@James\_Burrows](https://forum.kx.com/u/James_Burrows)\
**Post date:** [April 18, 2018, 11:19am UTC](https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742/3 "2018-04-18T11:19:00Z")

</div>

Hi Ankur,

You can use xasc&nbsp;to sort data directly on disk:

[https://code.kx.com/wiki/Reference/xasc#Sorting\_data\_on\_disk](https://code.kx.com/wiki/Reference/xasc#Sorting%5C_data%5C_on%5C_disk)

You can wrap it in a lambda and use `.Q.par` to build the filepath for each partition and table you’re interested in and sort each of those based on the column you want:

&nbsp; &nbsp; q){[c;d;t;p]c xasc .Q.par[d;p;t]}[`time;`:.;`trade] each date

`:./2018.04.02/trade`:./2018.04.03/trade`:./2018.04.05/trade

It is worth pointing out, however, that usually on-disk data has the parted&nbsp;attribute (p) applied to the sym column, because queries are more likely to use the sym&nbsp;column after the date clause. This is evident in the functions .Q.dpft and .Q.hdpf where the f in each is the field to apply the p attribute to.

[https://code.kx.com/wiki/DotQ/DotQDothdpf](https://code.kx.com/wiki/DotQ/DotQDothdpf)

Hope this helps,  
James

---

<div class="post-metadata">

**Author:** ![bajajank](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bajajank](https://forum.kx.com/u/bajajank)\
**Post date:** [April 18, 2018, 6:48pm UTC](https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742/4 "2018-04-18T18:48:00Z")

</div>

Thanks for the reply James.=20 This worked for me. But as you had pointed out, the sym column lost it=E2= =80=99s parted attribute. So, are we saying we can have attribute only one = column? I tried to set the parted attribute back on sym column and it faile= d giving me u-fail error(invalid attempt to set unique attribute). Is there= any way to restore this attribute back? Lastly, I was planning to add the = parted attribute on couple of other columns to optimize the query further b= ut it seems it would reset the previous attribute? Thanks again.

---

<div class="post-metadata">

**Author:** ![bajajank](https://avatars.discourse-cdn.com/v4/letter/b/b38774/32.png) [@bajajank](https://forum.kx.com/u/bajajank)\
**Post date:** [April 18, 2018, 6:53pm UTC](https://forum.kx.com/t/sort-a-column-for-all-dates-for-a-partitoned-table/11742/5 "2018-04-18T18:53:00Z")

</div>

Thanks Flying. I checked my r.q and it seems I am using the standard script which has the following u.end: / end of day: save, clear, hdb reload .u.end:{t:tables`.;t@:where `g=attr each t@:`sym;.Q.hdpf[`$“:”,.u.x 1;`:.;x;`sym];@[;`sym;`g#] each t;}; Any suggestions on how this can be modified to sort on certain column before save it down to disk?
