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
Distinct On authored Apr 11, 2017 by Carlos's avatar Carlos
Hide whitespace changes
Inline Side-by-side
Distinct-On.md
View page @ 15df685b
......@@ -5,6 +5,7 @@ MySQL-like group by statement on queries. It keeps only the first row of each se
distinct_on(*columns)
```
Just send the list of fields to be included on the `DISTINCT ON` statement:
```ruby
# SELECT DISTINCT ON ( "users"."name" ) "users".* FROM "users"
User.distinct_on(:name).all
......@@ -12,6 +13,9 @@ 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
# SELECT DISTINCT ON ( "photos"."type" ) "users".* FROM "users" INNER JOIN "photos" ON "users"."id" = "photos"."user_id"
User.joins(:photos).distinct_on(photos: :type).all
# SELECT DISTINCT ON ( "photos"."type", "photos"."size" ) "users".* FROM "users" INNER JOIN "photos" ON "users"."id" = "photos"."user_id"
User.joins(:photos).distinct_on(photos: [:type, :size]).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