DQL: How to group by inside group by?

Hi,
I have this query

{
  dataCalculation(func: has(users.id)) {
    users.id

    users.friends {
       ~country.friends @groupby(country.id) {
      	countryCount: count(uid)
      }
    }
  }
}

And in result I see

{
  "data": {
    "dataCalculation": [
      {
        "users.id": "3a0a8bff-71c5-4e5f-bac1-52db4864e870",
        "users.friends": [
          {
            "~country.friends": [
              {
                "@groupby": [
                  {
                    "country.id": "abcd1",
                    "countryCount": 1
                  },
                  {
                    "country.id": "abcd2",
                    "countryCount": 1
                  }
                ]
              }
            ]
          },
          {
            "~country.friends": [
              {
                "@groupby": [
                  {
                    "country.id": "abcd1",
                    "countryCount": 1
                  },
                  {
                    "country.id": "abcd2",
                    "countryCount": 1
                  }
                ]
              }
            ]
          }
        .....

Question: How can I group by elements in “@groupby”? to make result

{
            "~country.friends": [
              {
                "@groupby": [
                  {
                    "country.id": "abcd1",
                    "countryCount": 2
                  },
                  {
                    "country.id": "abcd2",
                    "countryCount": 2
                  }
                ]
              }
            ]
          }

Hello Devid,

Since you’re applying groupby to country.id which is a uid predicate, you can capture and save the count in a variable and use it in a different function.

{ 
  var(func: has(users.id)) {
    users.id

    users.friends {
       ~country.friends @groupby(country.id) {
      	c as count(uid)
      }
    }
  }

  byCountry(func: uid(c), orderasc: val(c)) {
    uid
    countryCount: val(c)
  }
}

Can you check if this works for you ?