# Sort by count

{
"set": [
{
"uid": "_:org1",
"[email protected]": "organization_a",
"social_networks": [
{
"[email protected]": "twitter"
},
{
"[email protected]": "telegram"
},
{
"[email protected]": "facebook"
}
]
},
{
"uid": "_:org2",
"[email protected]": "organization_b",
"social_networks": [
{
"[email protected]": "twitter"
},
{
"[email protected]": "telegram"
}
]
},
{
"uid": "_:org3",
"[email protected]": "organization_c",
"social_networks": [
{
"[email protected]": "twitter"
},
{
"[email protected]": "telegram"
}
]
}
]
}
1. How to sort by count of social_networks? count(social_networks)?
â€ścount(social_networks)â€ť: 3
â€ścount(social_networks)â€ť: 2
â€ścount(social_networks)â€ť: 2
2. How to group by values of count(social_networks) and get counts of organizations
â€ścount(social_networks)â€ť: 3 - 1 organization
â€ścount(social_networks)â€ť: 2 - 2 organizations
{
socialmedia(func: uid(0x18b3879,0x18b387a,0x18b387b)) {
uid
[email protected]
}
}

## DATA

{
"data": {
"socialmedia": [
{
"uid": "0x18b3879",
"[email protected]": "twitter"
},
{
"uid": "0x18b387a",
"[email protected]": "telegram"
},
{
"uid": "0x18b387b",
"[email protected]": "facebook"
}
]
}
}

# dataset

{
"set": [
{
"uid": "_:org1",
"[email protected]": "organization_a",
"social_networks": [
{
"uid": "0x18b3879"
},
{
"uid": "0x18b387a"
},
{
"uid": "0x18b387b"
}
]
},
{
"uid": "_:org2",
"[email protected]": "organization_b",
"social_networks": [
{
"uid": "0x18b3879"
},
{
"uid": "0x18b387a"
}
]
},
{
"uid": "_:org3",
"[email protected]": "organization_c",
"social_networks": [
{
"uid": "0x18b3879"
},
{
"uid": "0x18b387a"
}
]
}
]
}
{
socialmedia(func: has(social_networks)) {
uid
[email protected]
count(social_networks)
}
}

{
"data": {
"socialmedia": [
{
"uid": "0x18b387c",
"[email protected]": "organization_c",
"count(social_networks)": 2
},
{
"uid": "0x18b387d",
"[email protected]": "organization_a",
"count(social_networks)": 3
},
{
"uid": "0x18b387e",
"[email protected]": "organization_b",
"count(social_networks)": 2
}
]
}
}

Not possible like this.

{
var(func: has(<~social_networks>)) @groupby(<~social_networks>) {
a as count(uid)
}

byGenre(func: uid(a), orderdesc: val(a)) {
[email protected]
social_networks : val(a)
}
}

Response

{
"data": {
"byGenre": [
{
"[email protected]": "organization_a",
"social_networks": 3
},
{
"[email protected]": "organization_c",
"social_networks": 2
},
{
"[email protected]": "organization_b",
"social_networks": 2
}
]
}
}
1 Like

use a block defining a var and then order using the value of the var.
Example with donor dataset (available in benchmark repo) where schools have many projects.

{
var(func:type(School),first:10) {
c as count(~project_school)
}
list(func:uid(c),orderdesc:val(c)){
name
numberOfProjects:count(~project_school)
}
}

Will give you the kind of aggregation you are looking for (from what I understand).

{
"data": {
"list": [
{
"name": "Delaware Elementary Equity School",
"numberOfProjects": 16
},
{
"name": "L E Monahan Elementary School",
"numberOfProjects": 14
},
{
"name": "Philadelphia Performing Arts Charter School - West Campus",
"numberOfProjects": 8
},...
1 Like