Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get max and min value using GraphQL query

I have been working on a GraphQL and firing queries to get the JSON response into react app.

But I need to fire a query which will return me the max and min value from the column.

I have gone through the document of GraphQL but nowhere I found the way to accomplish this.

{
  schneider(where: {_and: [{device_time: {_gte: "2018-09-04T20:11:42.097+05:30"}}, {device_time: {_lte: "2018-09-04T20:12:43.187+05:30"}}]}) {
    active_energy_received
  }
}

The above query gives me the list of values that are between the two provided dates.

{
  "data": {
    "schneider": [
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0.82099998
      },
      {
        "active_energy_received": 4.3699998856
      },
      {
        "active_energy_received": 0
      },
      {
        "active_energy_received": 0.820999979972839
      },
      {
        "active_energy_received": 4.3699998856
      }
    ]
  }
}

Now, instead of all the values, I want only max and min value from this list.

UPDATE:

I am using hasura.io with postgreSQL.

like image 751
Vishal Shetty Avatar asked Jul 28 '26 23:07

Vishal Shetty


1 Answers

If you are using Hasura.io then you can use the customized API queries to ORDER BY and LIMIT for the MIN and MAX result.

Example for MAX value:

query {
  table_name(limit: 1, order_by: {time: desc}) { 
      data 
      time
    }
}

Example for MIN value:

query {
  table_name(limit: 1, order_by: {time: asc}) { 
      data 
      time
    }
}
like image 127
Dimitrios Mavrommatis Avatar answered Aug 01 '26 09:08

Dimitrios Mavrommatis