Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hasura order by date with distinct

I have a course table, each course has an id, a code and a start date. Example:

course_id    course_code    start_date
uuid1        course_a       2021-06-01
uuid2        course_a       2021-09-01
uuid3        course_b       2021-06-01
uuid4        course_b       2021-09-01

I want to get the latest record of each distinct course and I can do it in SQL

SELECT DISTINCT ON (course_code)
    course_id,
    course_code,
    start_date
FROM course
ORDER BY course_code, start_date DESC

uuid2        course_a       2021-09-01
uuid4        course_b       2021-09-01

But is it possible to query in graphql?

like image 375
AARon Avatar asked Nov 25 '25 07:11

AARon


1 Answers

Solved by adding a list of order_by condition instead of unordered object

query MyQuery {
  course(distinct_on: course_code, order_by: [{course_code: asc}, {start_date: desc}]) {
    course_id
    course_code,
    start_date
  }
}
like image 137
AARon Avatar answered Nov 26 '25 23:11

AARon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!