Aggregating only top x nodes

Is any way to do a groupby of a predicate but only for the top x nodes on each group.

For example if I have 2 nodes: Donations and State

The Donations node has an amount attribute.

I would like to query the sum of the top 10 donations per state.

Any help will be appreciated.

Thanks,
Marcelo

Yes,

you can use multiple blocks: https://docs.dgraph.io/query-language#multiple-query-blocks

With Var Block: https://docs.dgraph.io/query-language/#var-blocks

with Variables: https://docs.dgraph.io/query-language/#query-variables

Aggregation itself: https://docs.dgraph.io/query-language/#max

and Math https://docs.dgraph.io/query-language/#math-on-value-variables

I have been working with all those concepts you outline but I can’t figure how to pick the top highest values for each group. Once the aggregation is done, you can’t do any filtering on it. So I guess I will have to filter before that but I want to do filtering with grouping at the same time and that does not seem to work.

Is any way you can provide a sample.

Thanks,
Marcelo

have you tried these

https://docs.dgraph.io/query-language/#pagination

https://docs.dgraph.io/query-language/#sorting

Yes. I have done sorting of all the donations but I have to groupby state but I don’t know how to indicate to group only the top 10 of each group.

Once the aggregation is done, I don’t see a way to filter the components making up the aggregation.

Thanks,
Marcelo

when you did groupby and you can also add sorting too, this can get top k group, if you want to get top k in each group, you need to use sort again in each group

That is my problem. Once you do the aggregation (grouping), you can’t do any other filtering on it.

Thanks

yeah, it might concern about you real data, and then think about the query

i tried this, it works. hope it helps.

  1. mutate my data like:

     _:class1 <name> "class1" .
     _:class2 <name> "class2" .
    
     _:st_sype1 <name> "strudent_type_one" .
     _:st_sype2 <name> "strudent_type_two" .
    
     _:st1 <name> "student1" .
     _:st1 <type> _:st_sype1 .
     _:st1 <grade> "3.8" . 
     _:st2 <name> "student2" .
     _:st2 <type> _:st_sype1 .
     _:st2 <grade> "4.8" .
     _:st3 <name> "student3" .
     _:st3 <type> _:st_sype1 .
     _:st3 <grade> "5.8" .
    
     _:st4 <name> "student4" .
     _:st4 <type> _:st_sype2 .
     _:st4 <grade> "6.8" .
     _:st5 <name> "student5" .
     _:st5 <type> _:st_sype2 .
     _:st5 <grade> "7.8" .
     _:st6 <name> "student6" .
     _:st6 <type> _:st_sype2 .
     _:st6 <grade> "8.8" .	
    
     _:class1 <student>  _:st1 .
     _:class1 <student>  _:st2 .
     _:class1 <student>  _:st3 .
    
     _:class2 <student>  _:st4 .
     _:class2 <student>  _:st5 .
     _:class2 <student>  _:st6 .
    
  2. groupby type and get top k student in each group

{
  var(func:uid(0x7, 0x2)) {
    student @groupby(type) {
      a as count(uid)
    }
  }
  
  byTypequery(func:uid(a)) {
    number : val(a)
    name
    ~type (orderdesc: grade, first:2) {
      name
      grade
      type {
        name
      }
   	}
  }
}
  1. result of this query
{
  "data": {
    "byTypequery": [
      {
        "number": 3,
        "name": "strudent_type_one",
        "~type": [
          {
            "name": "student3",
            "grade": 5.8,
            "type": [
              {
                "name": "strudent_type_one"
              }
            ]
          },
          {
            "name": "student2",
            "grade": 4.8,
            "type": [
              {
                "name": "strudent_type_one"
              }
            ]
          }
        ]
      },
      {
        "number": 3,
        "name": "strudent_type_two",
        "~type": [
          {
            "name": "student6",
            "grade": 8.8,
            "type": [
              {
                "name": "strudent_type_two"
              }
            ]
          },
          {
            "name": "student5",
            "grade": 7.8,
            "type": [
              {
                "name": "strudent_type_two"
              }
            ]
          }
        ]
      }
    ]
  },
  "extensions": {
    "server_latency": {
      "parsing_ns": 96397,
      "processing_ns": 4301956,
      "encoding_ns": 1172687
    },
    "txn": {
      "start_ts": 38,
      "lin_read": {
        "ids": {
          "1": 13
        }
      }
    }
  }
}

PS: class1’s uid is 0x7 and class2’s 0x2 . i am sure you have noticed this.

@shanghai-Jerry Thanks. I will give a try.