# Help with Postgre, GROUP BY and ORDER BY

**URL:** <https://www.ruby-forum.com/t/help-with-postgre-group-by-and-order-by/175128>\
**Category:** Rails\
**Created:** [September 8, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/help-with-postgre-group-by-and-order-by/175128 "2009-09-08T15:01:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 8, 2009, 3:01pm UTC](https://www.ruby-forum.com/t/help-with-postgre-group-by-and-order-by/175128/1 "2009-09-08T15:01:31Z")

</div>

I want to obtain a array group by a field\_1 of a table but ordered by a  
field\_2 of a table, with one line of code. How can I do that using  
Postgre? I’ll appreciate any help

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 8, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/help-with-postgre-group-by-and-order-by/175128/2 "2009-09-08T15:03:35Z")

</div>

John S. wrote:

> I want to obtain a array group by a field\_1 of a table but ordered by a  
> field\_2 of a table, with one line of code. How can I do that using  
> Postgre? I’ll appreciate any help

Also, one of the conditions of the SQL will user some pagination params  
from a plugin

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 8, 2009, 3:45pm UTC](https://www.ruby-forum.com/t/help-with-postgre-group-by-and-order-by/175128/3 "2009-09-08T15:45:17Z")

</div>

John S. wrote:

> John S. wrote:
> 
> > I want to obtain a array group by a field\_1 of a table but ordered by a  
> > field\_2 of a table, with one line of code. How can I do that using  
> > Postgre? I’ll appreciate any help
> 
> Also, one of the conditions of the SQL will user some pagination params  
> from a plugin

Once again, already solved. Using subqueries.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 8, 2009, 6:52pm UTC](https://www.ruby-forum.com/t/help-with-postgre-group-by-and-order-by/175128/4 "2009-09-08T18:52:28Z")

</div>

John S. wrote:

> John S. wrote:
> 
> > John S. wrote:
> > 
> > > I want to obtain a array group by a field\_1 of a table but ordered by a  
> > > field\_2 of a table, with one line of code. How can I do that using  
> > > Postgre? I’ll appreciate any help
> > 
> > Also, one of the conditions of the SQL will user some pagination params  
> > from a plugin
> 
> Once again, already solved. Using subqueries.

Why? I believe you should just be able to do  
GROUP BY field\_1  
ORDER BY field\_1, field\_2

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
