I'm trying to find how many children a parent has, from the data below. Here level is the level and parent is the id of the row it's linked to. I'm trying to get data outputted, arranged by level and the number of children.
I pieced the below sql, but this doesn't really return results the way I want:
select id, level, count(parent) as p, country
from stats group by level order by level asc, p desc;
Can you help?
Table Data
"id" "level" "parent" "country"
"1" "1" "0" "US"
"2" "2" "1" "US"
"3" "2" "1" "US"
"4" "2" "1" "US"
"5" "2" "1" "US"
"6" "3" "2" "US"
"7" "3" "2" "US"
"8" "1" "0" "US"
"9" "2" "8" "US"
"10" "2" "8" "US"
"11" "2" "8" "US"
"12" "3" "9" "US"
"13" "3" "9" "US"
"14" "3" "9" "US"
"15" "3" "10" "US"
"16" "3" "10" "US"
"17" "3" "10" "US"
"18" "3" "10" "US"
Desired Output
"id" "type" "country" "Count"
"1" "1" "US" "4"
"8" "1" "US" "3"
"10" "2" "US" "4"
"9" "2" "US" "3"
"2" "2" "US" "2"