2019-01-25 06:31:54 +03:00
|
|
|
Distinct query results
|
|
|
|
======================
|
2018-11-23 04:53:56 +03:00
|
|
|
|
2018-12-03 15:12:24 +03:00
|
|
|
.. contents:: Table of contents
|
|
|
|
:backlinks: none
|
|
|
|
:depth: 1
|
|
|
|
:local:
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
You can fetch rows with only distinct values of a column using the ``distinct_on`` argument. The first ``order_by``
|
|
|
|
columns must match the ``distinct_on`` column. See :doc:`sort queries <sorting>` for more info on ``order_by``.
|
2018-11-23 04:53:56 +03:00
|
|
|
|
|
|
|
.. code-block:: graphql
|
|
|
|
|
|
|
|
employee (
|
|
|
|
distinct_on: [employee_select_column]
|
|
|
|
order_by: [employee_order_by]
|
|
|
|
): [employee]!
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
# select column enum type for "employee" table
|
2018-11-23 04:53:56 +03:00
|
|
|
enum employee_select_column {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
department
|
|
|
|
salary
|
|
|
|
}
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
Fetch results with distinct values of a particular field
|
|
|
|
--------------------------------------------------------
|
|
|
|
|
|
|
|
**For example**, fetch highest salaried employee from each department:
|
2018-11-23 04:53:56 +03:00
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
:view_only:
|
|
|
|
:query:
|
|
|
|
query {
|
|
|
|
employee(
|
|
|
|
distinct_on: [department]
|
|
|
|
order_by: [{department: asc}, {salary: desc}]
|
|
|
|
){
|
|
|
|
id
|
|
|
|
name
|
|
|
|
department
|
|
|
|
salary
|
|
|
|
}
|
|
|
|
}
|
|
|
|
:response:
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"employee": [
|
|
|
|
{
|
|
|
|
"id": 5,
|
|
|
|
"name": "Kamila",
|
|
|
|
"department": "Engineering",
|
|
|
|
"salary": 4325
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 4,
|
|
|
|
"name": "Damien",
|
|
|
|
"department": "Product",
|
|
|
|
"salary": 3124
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 7,
|
|
|
|
"name": "Rickard",
|
|
|
|
"department": "Services",
|
|
|
|
"salary": 2223
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|