<?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; sql</title>
	<atom:link href="http://www.pastbedti.me/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pastbedti.me</link>
	<description>About ruby, rails, postgresql and stuff....</description>
	<lastBuildDate>Fri, 02 Apr 2010 20:12:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 a [...]]]></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>
<p>[cc lang="ruby"]<br />
User.count(:group => &#8220;DATE(created_at)&#8221;)<br />
[/cc]</p>
<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>
<p>[cc lang="ruby"]<br />
>> User.count(:order => &#8216;created_at DESC&#8217;, :group => ["DATE(created_at)"])<br />
ActiveRecord::StatementInvalid: PGError: ERROR:  column &#8220;users.created_at&#8221; must appear in the GROUP BY clause or be used in an aggregate function<br />
: SELECT count(*) AS count_all, DATE(created_at) AS date_created_at FROM &#8220;users&#8221;  GROUP BY DATE(created_at)  ORDER BY created_at DESC<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:212:in `log&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:507:in `execute&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:985:in `select_raw&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:972:in `select&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:255:in `execute_grouped_calculation&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:132:in `calculate&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `catch&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:130:in `calculate&#8217;<br />
        from /tmp/app/vendor/rails/activerecord/lib/active_record/calculations.rb:48:in `count&#8217;<br />
        from (irb):18<br />
[/cc]</p>
<p>And this is the right way to do it:</p>
<p>[cc lang="ruby"]<br />
>> User.count(:order => &#8216;DATE(created_at) DESC&#8217;, :group => ["DATE(created_at)"])<br />
=> #<OrderedHash {"2009-10-14"=>22, &#8220;2009-10-25&#8243;=>4, &#8220;2009-11-04&#8243;=>8, &#8220;2009-10-15&#8243;=>9, &#8220;2009-10-26&#8243;=>16, &#8220;2009-11-05&#8243;=>9, &#8220;2009-10-16&#8243;=>193, &#8220;2009-10-27&#8243;=>14, &#8220;2009-11-06&#8243;=>9, &#8220;2009-10-17&#8243;=>49, &#8220;2009-10-28&#8243;=>15, &#8220;2009-11-07&#8243;=>6, &#8220;2009-10-18&#8243;=>36, &#8220;2009-10-29&#8243;=>8, &#8220;2009-10-19&#8243;=>116, &#8220;2009-10-30&#8243;=>15>[/cc]</p>
<p>As you may notice the Hash does not look ordered. But if you loop over it you will get them in order:</p>
<p>[cc lang='ruby']<br />
>> User.count(:order => &#8216;DATE(created_at) DESC&#8217;, :group => ["DATE(created_at)"]).each {|u| puts &#8220;#{u[0]} -> #{u[1]}&#8221; }<br />
2009-11-07 -> 6<br />
2009-11-06 -> 9<br />
2009-11-05 -> 9<br />
2009-11-04 -> 8<br />
2009-11-03 -> 14<br />
2009-11-02 -> 20<br />
2009-11-01 -> 10<br />
2009-10-31 -> 6<br />
2009-10-30 -> 15<br />
2009-10-29 -> 8<br />
..<br />
[/cc]</p>
<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>0</slash:comments>
		</item>
	</channel>
</rss>
