Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • T torque-postgresql
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 6
    • Issues 6
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 1
    • Merge requests 1
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • Carlos
  • torque-postgresql
  • Wiki
  • Distinct On

Distinct On · Changes

Page history
First version authored Apr 11, 2017 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Distinct-On.md 0 → 100644
View page @ 67b7072c
MySQL-like group by statement on queries. It keeps only the first row of each set of rows where the given expressions evaluate to equal. [PostgreSQL Docs](https://www.postgresql.org/docs/9.5/static/sql-select.html#SQL-DISTINCT)
## How it works
```ruby
distinct_on(*columns)
```
```ruby
# SELECT DISTINCT ON ( "users"."name" ) "users".* FROM "users"
User.distinct_on(:name).all
```
You can use where-like syntax to find more complex columns:
```ruby
# SELECT DISTINCT ON ( "users"."name" ) "users".* FROM "users"
User.distinct_on(:name).all
```
\ No newline at end of file
Clone repository
  • Arel
  • Auxiliary Statements
  • Belongs to Many
  • Box
  • Circle
  • Configuring
  • Date Time Range
  • Distinct On
  • Dynamic Attributes
  • Enum Set
  • Enum
  • Has Many
  • Home
  • Inherited Tables
  • Insert All
View All Pages