# How to group counts for ends of a graph

**URL:** <https://discuss.dgraph.io/t/how-to-group-counts-for-ends-of-a-graph/12379>\
**Category:** Dgraph\
**Tags:** kind:question\
**Created:** [January 20, 2021, 5:43pm UTC](https://discuss.dgraph.io/t/how-to-group-counts-for-ends-of-a-graph/12379 "2021-01-20T17:43:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![deanroker123](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/deanroker123/32/5332_2.png) [@deanroker123](https://discuss.dgraph.io/u/deanroker123)\
**Post date:** [January 20, 2021, 5:43pm UTC](https://discuss.dgraph.io/t/how-to-group-counts-for-ends-of-a-graph/12379/1 "2021-01-20T17:43:19Z")

</div>

Hi,  
What I want to do is hard to explain  
I am trying to sum the count of edges on a node in the middle of a graph but to have the aggregate for unique sets of end points.  
Here is a query

```auto
{
  g(func: type(Theme)){
    theme.name
    search{
      querystring
      result{
        count(result.context)
        document{
          doc.id
        }
      }
    }
  }
}

```

That returns

```auto
{
  "data": {
    "g": [
      {
        "theme.name": "test theme",
        "search": [
          {
            "querystring": "improving",
            "result": [
              {
                "count(result.context)": 1,
                "document": [
                  {
                    "doc.id": 2443317
                  }
                ]
              },

```

What I want to do is add up the number count(result.context) for each theme and doc, so a theme has 3 searches that all point to the same doc I want the total result.context for that theme on that doc.

Hope that makes sense. Any ideas?

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [January 20, 2021, 5:55pm UTC](https://discuss.dgraph.io/t/how-to-group-counts-for-ends-of-a-graph/12379/2 "2021-01-20T17:55:04Z")

</div>

I wrote a function to "normalize deep nested objects like this…

> [@Flatten Utility Function](http://discuss.hypermode.com/t/flatten-utility-function/12306):
>
> I found myself with a compound results from a GraphQL query that I needed to flatten to a single list. More specifically, I would do a nested query to get all of the ids linked to a higher type going through a few one to many relationships. The results I retrieved would look something like: const response = { data: { queryFoo: [{ bar: { baz: [{ id: "0x1" }, { id: "0x2" }] } }, { bar: { baz:…

---

<div class="post-metadata">

**Author:** ![deanroker123](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/deanroker123/32/5332_2.png) [@deanroker123](https://discuss.dgraph.io/u/deanroker123)\
**Post date:** [January 20, 2021, 6:07pm UTC](https://discuss.dgraph.io/t/how-to-group-counts-for-ends-of-a-graph/12379/3 "2021-01-20T18:07:59Z")

</div>

So you did it in the app rather than through the query? That is my backup plan. But If there is a way I can do it just with the query that would be super cool.

Thanks for the idea though.

---

<div class="post-metadata">

**Author:** ![amaster507](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/amaster507/32/4123_2.png) [@amaster507](https://discuss.dgraph.io/u/amaster507)\
**Post date:** [January 20, 2021, 6:16pm UTC](https://discuss.dgraph.io/t/how-to-group-counts-for-ends-of-a-graph/12379/4 "2021-01-20T18:16:20Z")

</div>

my plan to use this is currently in progress, but it will be client side with a thought about maybe putting it in a lambda function.
