Related discussions
How to groupby date - #7 by labs20
Running total (cumulative sum) by date - #4 by MichelDiz
What is the technical reason we can't store variables of @groupby unless it uses "uid"?
The GroupBy function today only works by UID, but this term “group by” is used in other database technologies in an expressive and unrestricted way. Where we can group results by values and not only relationships. This is a very important feature. And it seems that a single step is missing for the Dgraph also support it. For Dgraph seems to be able to do this.
The query below works perfectly. Where I group by a predicate containing attributes. Taking the example “First name”. It could be “datetime (hour, day, month, year)”, “Score”, “age”, “Tag”, “status (New, updated, outdated, deleted, refurbished / status code” and so on.
Of course the “first name” example is very hypothetical to be used in real life. Consider the other possibilities.
However, the query does not allow me to create a second block. To which I would treat the results better. And get a desired result.
{
q(func: has(group_name)) {
group_name
member @groupby(first_name) {
Total_first_name : count(uid)
}
}
}
{
"data": {
"q": [
{
"group_name": "Some Group",
"member": [
{
"@groupby": [
{
"first_name": "Michel",
"Total_first_name": 6
}
]
}
]
},
{
"group_name": "Some Group 2",
"member": [
{
"@groupby": [
{
"first_name": "Daniel",
"Total_first_name": 3
},
{
"first_name": "Lucas",
"Total_first_name": 3
}
]
}
]
}
]
}
The desired result
Update in FOREACH func in DQL (loops in Bulk Upsert) - #5 by MichelDiz
{
var(func: has(group_name)) {
member @groupby(first_name) {
T as count(uid)
}
}
q(func: uid(T)) {
first_name
last_name
Total_first_name : val(T)
}
}
The Result
{
"data": {
"q": [
{
"Michel": [
{
"Total_first_name": 6
},
{
"uid": "0x1",
"first_name": "Michel"
},
{
"uid": "0x6",
"first_name": "Michel"
},
{
"uid": "0x8",
"first_name": "Michel"
},
{
"uid": "0x9",
"first_name": "Michel"
},
{
"uid": "0xf",
"first_name": "Michel"
},
{
"uid": "0xa",
"first_name": "Michel"
}
]
},
{
"Daniel": [
{
"Total_first_name": 3
},
{
"uid": "0x2",
"first_name": "Daniel"
},
{
"uid": "0xf22",
"first_name": "Daniel"
},
{
"uid": "0xf4",
"first_name": "Daniel"
}
]
},
{
"Lucas": [
{
"Total_first_name": 3
},
{
"uid": "0x3",
"first_name": "Lucas"
},
{
"uid": "0xf44",
"first_name": "Lucas"
},
{
"uid": "0xf77",
"first_name": "Lucas"
}
]
}
]
}
Schema and Mutation
<member>: [uid] @count @reverse .
{
set {
_:InGroup <group_name> "Some Group" .
_:InGroup <member> _:u .
_:InGroup <member> _:u2 .
_:InGroup <member> _:u3 .
_:InGroup <member> _:u4 .
_:InGroup <member> _:u5 .
_:InGroup <member> _:u6 .
_:u <user> "" .
_:u <first_name> "Michel" .
_:u <last_name> "A" .
_:u2 <user> "" .
_:u2 <first_name> "Michel" .
_:u2 <last_name> "B" .
_:u3 <user> "" .
_:u3 <first_name> "Michel" .
_:u3 <last_name> "C" .
_:u4 <user> "" .
_:u4 <first_name> "Michel" .
_:u4 <last_name> "D" .
_:u5 <user> "" .
_:u5 <first_name> "Michel" .
_:u5 <last_name> "E" .
_:u6 <user> "" .
_:u6 <first_name> "Michel" .
_:u6 <last_name> "F" .
_:InGroup2 <group_name> "Some Group 2" .
_:InGroup2 <member> _:uB .
_:InGroup2 <member> _:u2B .
_:InGroup2 <member> _:u3B .
_:InGroup2 <member> _:u4B .
_:InGroup2 <member> _:u5B .
_:InGroup2 <member> _:u6B .
_:uB <user> "" .
_:uB <first_name> "Lucas" .
_:uB <last_name> "A" .
_:u2B <user> "" .
_:u2B <first_name> "Lucas" .
_:u2B <last_name> "B" .
_:u3B <user> "" .
_:u3B <first_name> "Lucas" .
_:u3B <last_name> "C" .
_:u4B <user> "" .
_:u4B <first_name> "Daniel" .
_:u4B <last_name> "D" .
_:u5B <user> "" .
_:u5B <first_name> "Daniel" .
_:u5B <last_name> "E" .
_:u6B <user> "" .
_:u6B <first_name> "Daniel" .
_:u6B <last_name> "F" .
}
}