# Segmented tables

**URL:** <https://forum.kx.com/t/segmented-tables/8853>\
**Category:** Community Support\
**Tags:** kdb-and-q\
**Created:** [May 29, 2014, 2:29pm UTC](https://forum.kx.com/t/segmented-tables/8853 "2014-05-29T14:29:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![skuvvv1](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@skuvvv1](https://forum.kx.com/u/skuvvv1)\
**Post date:** [May 29, 2014, 2:29pm UTC](https://forum.kx.com/t/segmented-tables/8853/1 "2014-05-29T14:29:00Z")

</div>

Hello.  
I read about segmentation and it looks good for me.

I want to segment by intraday time, for hour eg.

Approach in documentation looks like:

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/9/2009.01.01/t2/ set ([] ti:09:30:00 09:31:00; s:`:/db2/sym?`ibm`t; p:101 17f)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/10/2009.01.01/t2/ set ([] ti:10:30:00 10:31:00; s:`:/db2/sym?`ibm`t; p:101.5 17.5)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/9/2009.01.02/t2/ set ([] ti:09:30:00 09:31:00; s:`:/db2/sym?`ibm`t; p:103 16.5f)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/10/2009.01.02/t2/ set ([] ti:10:30:00 10:31:00; s:`:/db2/sym?`ibm`t; p:102 17f)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/db2/par.txt 0: (“/dbdata/9”; “/dbdata/10”)

```
date ti s p -----------------------------2009.01.01 09:30:00 ibm 101 2009.01.01 09:31:00 t 17 2009.01.01 10:30:00 ibm 101.52009.01.01 10:31:00 t 17.5 2009.01.02 09:30:00 ibm 103 2009.01.02 09:31:00 t 16.5 2009.01.02 10:30:00 ibm 102 2009.01.02 10:31:00 t 17

```

I tried change it this way:

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/2009.01.01/9/t2/ set ([] ti:09:30:00 09:31:00; s:`:/db2/sym?`ibm`t; p:101 17f)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/2009.01.01/10/t2/ set ([] ti:10:30:00 10:31:00; s:`:/db2/sym?`ibm`t; p:101.5 17.5)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/2009.01.02/9/t2/ set ([] ti:09:30:00 09:31:00; s:`:/db2/sym?`ibm`t; p:103 16.5f)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/dbdata/2009.01.02/10/t2/ set ([] ti:10:30:00 10:31:00; s:`:/db2/sym?`ibm`t; p:102 17f)

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `:/db2/par.txt 0: (“/dbdata/2009.01.01”; “/dbdata/2009.01.02”)

But at result I have additional Column(Hour) and I haven’t Date column when loading table:

```
int ti s p ----------------------9 09:30:00 ibm 101 9 09:31:00 t 17 9 09:30:00 ibm 103 9 09:31:00 t 16.5 10 10:30:00 ibm 101.510 10:31:00 t 17.5 10 10:30:00 ibm 102 10 10:31:00 t 17

1)Can it possible to store data by my scheme?

2)How can I load chunk/segment etc(eg I want to load date=2009.01.02 and segment = 9, 10)?

```

---

<div class="post-metadata">

**Author:** ![l\_belshaw01](https://avatars.discourse-cdn.com/v4/letter/l/7ba0ec/32.png) [@l\_belshaw01](https://forum.kx.com/u/l_belshaw01)\
**Post date:** [May 29, 2014, 5:45pm UTC](https://forum.kx.com/t/segmented-tables/8853/2 "2014-05-29T17:45:00Z")

</div>

Hi Vadim,

So firstly, there are four types which you can partition by - year, month, date, and integer. You could write a function which casts the timestamp from your date-time columns to an integer, and rounds to the nearest hour. This would allow you to have the table partitioned on the hour in each day. Basically, something like this:

&nbsp;&nbsp;&nbsp; f:{`h xcols update h:`int$(`timestamp$date+time)% 0D01 from x}

&nbsp;&nbsp;&nbsp; table: ( date:2009.01.01 2009.01.01; time: 09:30:00 09:31:00; s:`imb`t;p:101 17)  
&nbsp;&nbsp;&nbsp; f[table]  
&nbsp;&nbsp;&nbsp; h&nbsp;&nbsp;&nbsp;&nbsp; date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; time&nbsp;&nbsp;&nbsp;&nbsp; s&nbsp;&nbsp; p&nbsp; &nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; ---------------------------------  
&nbsp;&nbsp;&nbsp; 78922 2009.01.01 09:30:00 imb 101  
&nbsp;&nbsp;&nbsp; 78922 2009.01.01 09:31:00 t&nbsp;&nbsp; 17

You could then partition your database on the field h. This is going to give a lot of partitions though.

What could be an easier solution is just to partition your table on date, and then use something like this as your select -

&nbsp;&nbsp;&nbsp; select from table where date=2009.01.01, 9=`hh$time

Something maybe closer to what you had previously would be like this, partitioning on the integer hour and then on date:

&nbsp;&nbsp;&nbsp; `:/db/9/2009.01.01/t/ set update seg:`hh$ti from ( ti:09:30:00 09:31:00; s:`:/db/sym?`ibm`t; p:101 17f) &nbsp;&nbsp;&nbsp; `:/db/10/2009.01.01/t/ set update seg:`hh$ti from ([] ti:10:30:00 10:31:00; s:`:/db/sym?`ibm`t; p:101.5 17.5)  
&nbsp;&nbsp;&nbsp; `:/db/9/2009.01.02/t/ set update seg:`hh$ti from ( ti:09:30:00 09:31:00; s:`:/db/sym?`ibm`t; p:103 16.5f) &nbsp;&nbsp;&nbsp; `:/db/par.txt 0: (“/db/9”; “/db/10”)

Hope this helps.

Louise.

```
-- 

```

Dr Louise Belshawlouise.belshaw@aquaq.co.uk

---

<div class="post-metadata">

**Author:** ![skuvvv1](https://avatars.discourse-cdn.com/v4/letter/s/7ba0ec/32.png) [@skuvvv1](https://forum.kx.com/u/skuvvv1)\
**Post date:** [May 30, 2014, 8:40am UTC](https://forum.kx.com/t/segmented-tables/8853/3 "2014-05-30T08:40:00Z")

</div>

Thank you for solutions!  
I think I will compose my approach from it.
