# Group() and PostgreSQL strange behavior

**URL:** <https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975>\
**Category:** Rails\
**Created:** [March 2, 2011, 6:33pm UTC](https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975 "2011-03-02T18:33:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 2, 2011, 6:33pm UTC](https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975/1 "2011-03-02T18:33:39Z")

</div>

Here is a simple request:

Visit.group(“user\_id”)

And I get:

PGError: ERROR: column “visits.id” must appear in the GROUP BY clause  
or be used in an aggregate function

If I add visits.id to the group by clause, then it asks me to add  
another column, then another, then another. Basically I have to list all  
columns in the table!

What’s going wrong? From the Rails guide it seemed straightforward.

Thanks for your help

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 2, 2011, 6:51pm UTC](https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975/2 "2011-03-02T18:51:30Z")

</div>

Eureka!

The docs are incomplete. I obviously need to add a count, sum, etc  
method for the grouping to make sense.

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 2, 2011, 10:45pm UTC](https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975/3 "2011-03-02T22:45:14Z")

</div>

Actually, as I think about it, you shouldn’t have a Select \* in a Group  
By? You should select the column(s) you want to aggregate.

Check this out:

> <https://stackoverflow.com/questions/1769361/postgresql-group-by-different-from-mysql>

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 2, 2011, 10:37pm UTC](https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975/4 "2011-03-02T22:37:07Z")

</div>

I checked my install and received the same results. Looks like a  
PostgreSQL syntax thing. I’m new to PostgreSQL and have not looked at  
group by SQL syntax.

Off the top of my head, as a (non recommended) work around, you could  
insert a .select() with a column list, and it should work. That,  
however, is a bad solution for obvious reasons, but might work in a  
pinch.

I tried, and received the same result:

User.group(:first\_name)

The following worked:

User.select(:first\_name).group(:first\_name)

---

<div class="post-metadata">

**Author:** ![fipa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/9e8a1a/32.png) [@fipa](https://www.ruby-forum.com/u/fipa)\
**Post date:** [March 3, 2011, 4:33pm UTC](https://www.ruby-forum.com/t/group-and-postgresql-strange-behavior/203975/5 "2011-03-03T16:33:17Z")

</div>

> I tried, and received the same result:
> 
> User.group(:first\_name)

The thing is, we group for a reason. So PostgreSQL is expecting that  
reason to appear, e.g: count(), sum(), etc.
