<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>pastbedti.me &#187; date</title>
	<atom:link href="http://www.pastbedti.me/tag/date/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pastbedti.me</link>
	<description>About ruby, rails, postgresql and stuff....</description>
	<lastBuildDate>Wed, 01 Jun 2011 20:09:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Grouping a timestamp field by date in Ruby On Rails / PostgreSQL</title>
		<link>http://www.pastbedti.me/2009/11/grouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql/</link>
		<comments>http://www.pastbedti.me/2009/11/grouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 00:55:09 +0000</pubDate>
		<dc:creator>Mathias Stjernström</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[timestamp]]></category>

		<guid isPermaLink="false">http://www.pastbedti.me/?p=563</guid>
		<description><![CDATA[Recently I&#8217;ve been working a lot with dates and tonight I had a hard time finding information about how to group a model by date when the model only have a timestamp column. Lets pretend we have a user model with a created_at attribute which is stored as a timestamp. If we want to plot [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I&#8217;ve been working a lot with dates and tonight I had a hard time finding information about how to group a model by date when the model only have a timestamp column. </p>
<p>Lets pretend we have a user model with a created_at attribute which is stored as a timestamp.</p>
<p>If we want to plot a simple graph showing signups per date this is the way to extract them:</p>
<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;DATE(created_at)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></td></tr></tbody></table></div>
<p>Thats all there is to it. From this you will get an OrderedHash containing the date and count of users for each date. </p>
<p>One small thing to remember if you are using PostgreSQL. You will need the DATE() function in any ORDER/SELECT statements because PostgreSQL will only select/order by fields thats in the GROUP BY statement and this is a  pretty healthy behavior that many databases (no names here) don&#8217;t care about. Think about it. If you are grouping all users by date and use * as selector how would the database know which username or email address to display?</p>
<p>This is what happens if you try to group by created_at without DATE() -></p>
<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'created_at DESC'</span>, <span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;DATE(created_at)&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color:#6666ff; font-weight:bold;">ActiveRecord::StatementInvalid</span>: PGError: ERROR: &nbsp;column <span style="color:#996600;">&quot;users.created_at&quot;</span> must appear <span style="color:#9966CC; font-weight:bold;">in</span> the GROUP BY clause <span style="color:#9966CC; font-weight:bold;">or</span> be used <span style="color:#9966CC; font-weight:bold;">in</span> an aggregate function<br />
: <span style="color:#CC0066; font-weight:bold;">SELECT</span> count<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span><span style="color:#006600; font-weight:bold;">&#41;</span> AS count_all, DATE<span style="color:#006600; font-weight:bold;">&#40;</span>created_at<span style="color:#006600; font-weight:bold;">&#41;</span> AS date_created_at FROM <span style="color:#996600;">&quot;users&quot;</span> &nbsp;GROUP BY DATE<span style="color:#006600; font-weight:bold;">&#40;</span>created_at<span style="color:#006600; font-weight:bold;">&#41;</span> &nbsp;ORDER BY created_at DESC <br />
&nbsp; &nbsp; &nbsp; &nbsp; from <span style="color:#006600; font-weight:bold;">/</span>tmp<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>vendor<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>activerecord<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>active_record<span style="color:#006600; font-weight:bold;">/</span>connection_adapters<span style="color:#006600; font-weight:bold;">/</span>abstract_adapter.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">212</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`log'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `</span>execute<span style="color:#996600;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; from <span style="color:#006600; font-weight:bold;">/</span>tmp<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>vendor<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>activerecord<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>active_record<span style="color:#006600; font-weight:bold;">/</span>connection_adapters<span style="color:#006600; font-weight:bold;">/</span>postgresql_adapter.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">972</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`select'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `</span>select_all_without_query_cache<span style="color:#996600;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; from <span style="color:#006600; font-weight:bold;">/</span>tmp<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>vendor<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>activerecord<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>active_record<span style="color:#006600; font-weight:bold;">/</span>calculations.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">255</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`execute_grouped_calculation'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:132:in `</span>calculate<span style="color:#996600;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `catch'</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; from <span style="color:#006600; font-weight:bold;">/</span>tmp<span style="color:#006600; font-weight:bold;">/</span>app<span style="color:#006600; font-weight:bold;">/</span>vendor<span style="color:#006600; font-weight:bold;">/</span>rails<span style="color:#006600; font-weight:bold;">/</span>activerecord<span style="color:#006600; font-weight:bold;">/</span>lib<span style="color:#006600; font-weight:bold;">/</span>active_record<span style="color:#006600; font-weight:bold;">/</span>calculations.<span style="color:#9900CC;">rb</span>:<span style="color:#006666;">130</span>:<span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#996600;">`calculate'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:48:in `</span>count<span style="color:#996600;">'<br />
&nbsp; &nbsp; &nbsp; &nbsp; from (irb):18</span></div></td></tr></tbody></table></div>
<p>And this is the right way to do it:</p>
<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'DATE(created_at) DESC'</span>, <span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;DATE(created_at)&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#008000; font-style:italic;">#&lt;OrderedHash {&quot;2009-10-14&quot;=&gt;22, &quot;2009-10-25&quot;=&gt;4, &quot;2009-11-04&quot;=&gt;8, &quot;2009-10-15&quot;=&gt;9, &quot;2009-10-26&quot;=&gt;16, &quot;2009-11-05&quot;=&gt;9, &quot;2009-10-16&quot;=&gt;193, &quot;2009-10-27&quot;=&gt;14, &quot;2009-11-06&quot;=&gt;9, &quot;2009-10-17&quot;=&gt;49, &quot;2009-10-28&quot;=&gt;15, &quot;2009-11-07&quot;=&gt;6, &quot;2009-10-18&quot;=&gt;36, &quot;2009-10-29&quot;=&gt;8, &quot;2009-10-19&quot;=&gt;116, &quot;2009-10-30&quot;=&gt;15&gt;</span></div></td></tr></tbody></table></div>
<p>As you may notice the Hash does not look ordered. But if you loop over it you will get them in order:</p>
<div class="codecolorer-container ruby twitlight" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br /></div></td><td><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#006600; font-weight:bold;">&gt;&gt;</span> User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:order</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'DATE(created_at) DESC'</span>, <span style="color:#ff3333; font-weight:bold;">:group</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;DATE(created_at)&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>u<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;#{u[0]} -&gt; #{u[1]}&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>07 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">6</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>06 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">9</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>05 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">9</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>04 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">8</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>03 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">14</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>02 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">20</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">11</span><span style="color:#006600; font-weight:bold;">-</span>01 <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">10</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">31</span> <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">6</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">30</span> <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">15</span><br />
<span style="color:#006666;">2009</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">-</span><span style="color:#006666;">29</span> <span style="color:#006600; font-weight:bold;">-&gt;</span> <span style="color:#006666;">8</span><br />
..</div></td></tr></tbody></table></div>
<p>And thats how you group a timestamp field with just its date part.</p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pastbedti.me/2009/11/grouping-a-timestamp-field-by-date-in-ruby-on-rails-postgresql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

