2020-01-14 15:57:45 +03:00
|
|
|
.. meta::
|
|
|
|
:description: Make distinct queries with Hasura
|
|
|
|
:keywords: hasura, docs, query, distinct query
|
|
|
|
|
2020-03-11 22:42:36 +03:00
|
|
|
.. _distinct_queries:
|
|
|
|
|
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
|
2019-09-05 17:53:50 +03:00
|
|
|
:depth: 2
|
2018-12-03 15:12:24 +03:00
|
|
|
:local:
|
|
|
|
|
2019-12-15 16:02:10 +03:00
|
|
|
The **distinct_on** argument
|
|
|
|
----------------------------
|
|
|
|
|
2019-02-06 09:39:36 +03:00
|
|
|
You can fetch rows with only distinct values of a column using the ``distinct_on`` argument.
|
|
|
|
|
2020-01-20 14:47:49 +03:00
|
|
|
It is typically recommended to use ``order_by`` along with ``distinct_on`` to ensure we get predictable results
|
|
|
|
*(otherwise any arbitrary row with a distinct value of the column may be returned)*.
|
|
|
|
Note that the ``distinct_on`` column needs to be the first column in the ``order_by`` expression.
|
2020-03-11 22:42:36 +03:00
|
|
|
See :ref:`sort queries <sort_query_results>` for more info on using ``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-02-06 09:39:36 +03:00
|
|
|
You can see the complete specification of the ``distinct_on`` argument in the :ref:`API reference <DistinctOnExp>`.
|
|
|
|
|
2019-01-25 06:31:54 +03:00
|
|
|
Fetch results with distinct values of a particular field
|
|
|
|
--------------------------------------------------------
|
|
|
|
|
2019-09-11 10:17:14 +03:00
|
|
|
**For example**, fetch the employee with the highest salary from each department:
|
2018-11-23 04:53:56 +03:00
|
|
|
|
|
|
|
.. graphiql::
|
|
|
|
:view_only:
|
|
|
|
:query:
|
|
|
|
query {
|
2019-02-06 09:39:36 +03:00
|
|
|
employee (
|
2018-11-23 04:53:56 +03:00
|
|
|
distinct_on: [department]
|
2019-02-06 09:39:36 +03:00
|
|
|
order_by: [
|
|
|
|
{department: asc},
|
|
|
|
{salary: desc}
|
|
|
|
]
|
|
|
|
) {
|
2018-11-23 04:53:56 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|