<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Rounding datetimes and timestamps in PostgreSQL</title>
	<atom:link href="http://www.rightbrainnetworks.com/blog/rounding-datetimes-and-timestamps-in-postgresql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rightbrainnetworks.com/blog/rounding-datetimes-and-timestamps-in-postgresql/</link>
	<description></description>
	<lastBuildDate>Thu, 11 Aug 2011 13:49:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Jamie</title>
		<link>http://www.rightbrainnetworks.com/blog/rounding-datetimes-and-timestamps-in-postgresql/comment-page-1/#comment-691</link>
		<dc:creator>Jamie</dc:creator>
		<pubDate>Tue, 29 Jun 2010 13:34:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rightbrainnetworks.com/?p=163#comment-691</guid>
		<description>Interesting solution... Thanks for taking the time to write it up and post!</description>
		<content:encoded><![CDATA[<p>Interesting solution&#8230; Thanks for taking the time to write it up and post!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://www.rightbrainnetworks.com/blog/rounding-datetimes-and-timestamps-in-postgresql/comment-page-1/#comment-690</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Tue, 29 Jun 2010 08:42:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.rightbrainnetworks.com/?p=163#comment-690</guid>
		<description>Hi,

You may like this solution also...

== Solution #1

You could build a 60 rows table with two columns : &quot;minute&quot; and &quot;nearest&quot;. Those 60 rows are pre-computed. Make &quot;minute&quot; the PK. 

By joining, you can get the nearest increment, and then you can compute back the rounded timestamp.

create table report.nearest
(
	&quot;minute&quot; integer,
	nearest time,
	constraint nearest_pk primary key (&quot;minute&quot;)
) ;

--
-- To be done once, when application is deployed
--

insert into report.nearest values (0, time &#039;00:00&#039;) ;
insert into report.nearest values (1, time &#039;00:00&#039;) ;
...
insert into report.nearest values (14, time &#039;00:00&#039;) ;
insert into report.nearest values (15, time &#039;00:15&#039;) ;
insert into report.nearest values (16, time &#039;00:16&#039;) ;
...

create table report.billing
(
	tstamp   timestamp,
	whatever integer
) ;

insert into report.billing values(timestamp &#039;2001-02-16 20:14:12&#039;, 1) ;
insert into report.billing values(timestamp &#039;2001-02-16 20:15:34&#039;, 2) ;

select
	B.tstamp,
	N.nearest,
	date_trunc(&#039;hour&#039;, B.tstamp) + N.nearest as rounded
from
	report.billing B,
	report.nearest N
where
	extract(minute from B.tstamp) = N.&quot;minute&quot; ;

Results are :

 TSTAMP              NEAREST  ROUNDED
 ------------------- -------- -------------------
 2001-02-16 20:14:12 00:00:00 2001-02-16 20:00:00
 2001-02-16 20:15:34 00:15:00 2001-02-16 20:15:00

The underlying principle is simple: you can tradeoff storage for computing power (and way back). Here, storage is table report.nearest and computing power is your round_timestamp function.

Btw, in database terminology, I guess &quot;nearest&quot; is a dimension table and &quot;billing&quot; is the fact table.

I don&#039;t know if this solution &quot;is the most elegant&quot; as you state but it is &quot;full SQL&quot;: usually, this is to be prefered because database engines are better at &quot;crunching&quot; sets of data and optmizing SQL requests rather than running procedural code.

== Solution #2

If you manage lots of billing records within a batch process (I mean 100K or millions rows maybe), you may consider to compute more data ahead of the batch time window.

Maybe extract(minute from B.tstamp) could be precomputed in a new column when the billing record is created ? The effect on whole performance of the application could be good ... or worse, it depends on the environment and must be validated.

Note: this denormalizes the data model to get better performance and should be documented.


Hope this helps.</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>You may like this solution also&#8230;</p>
<p>== Solution #1</p>
<p>You could build a 60 rows table with two columns : &#8220;minute&#8221; and &#8220;nearest&#8221;. Those 60 rows are pre-computed. Make &#8220;minute&#8221; the PK. </p>
<p>By joining, you can get the nearest increment, and then you can compute back the rounded timestamp.</p>
<p>create table report.nearest<br />
(<br />
	&#8220;minute&#8221; integer,<br />
	nearest time,<br />
	constraint nearest_pk primary key (&#8220;minute&#8221;)<br />
) ;</p>
<p>&#8211;<br />
&#8211; To be done once, when application is deployed<br />
&#8211;</p>
<p>insert into report.nearest values (0, time &#8217;00:00&#8242;) ;<br />
insert into report.nearest values (1, time &#8217;00:00&#8242;) ;<br />
&#8230;<br />
insert into report.nearest values (14, time &#8217;00:00&#8242;) ;<br />
insert into report.nearest values (15, time &#8217;00:15&#8242;) ;<br />
insert into report.nearest values (16, time &#8217;00:16&#8242;) ;<br />
&#8230;</p>
<p>create table report.billing<br />
(<br />
	tstamp   timestamp,<br />
	whatever integer<br />
) ;</p>
<p>insert into report.billing values(timestamp &#8217;2001-02-16 20:14:12&#8242;, 1) ;<br />
insert into report.billing values(timestamp &#8217;2001-02-16 20:15:34&#8242;, 2) ;</p>
<p>select<br />
	B.tstamp,<br />
	N.nearest,<br />
	date_trunc(&#8216;hour&#8217;, B.tstamp) + N.nearest as rounded<br />
from<br />
	report.billing B,<br />
	report.nearest N<br />
where<br />
	extract(minute from B.tstamp) = N.&#8221;minute&#8221; ;</p>
<p>Results are :</p>
<p> TSTAMP              NEAREST  ROUNDED<br />
 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;- &#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
 2001-02-16 20:14:12 00:00:00 2001-02-16 20:00:00<br />
 2001-02-16 20:15:34 00:15:00 2001-02-16 20:15:00</p>
<p>The underlying principle is simple: you can tradeoff storage for computing power (and way back). Here, storage is table report.nearest and computing power is your round_timestamp function.</p>
<p>Btw, in database terminology, I guess &#8220;nearest&#8221; is a dimension table and &#8220;billing&#8221; is the fact table.</p>
<p>I don&#8217;t know if this solution &#8220;is the most elegant&#8221; as you state but it is &#8220;full SQL&#8221;: usually, this is to be prefered because database engines are better at &#8220;crunching&#8221; sets of data and optmizing SQL requests rather than running procedural code.</p>
<p>== Solution #2</p>
<p>If you manage lots of billing records within a batch process (I mean 100K or millions rows maybe), you may consider to compute more data ahead of the batch time window.</p>
<p>Maybe extract(minute from B.tstamp) could be precomputed in a new column when the billing record is created ? The effect on whole performance of the application could be good &#8230; or worse, it depends on the environment and must be validated.</p>
<p>Note: this denormalizes the data model to get better performance and should be documented.</p>
<p>Hope this helps.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

