我试图从下面的数据中找出父母有多少孩子。这level
是level
和父级是id
它row
链接到的。我正在尝试输出数据,按级别和孩子的数量排列。
我拼凑了下面的 sql,但这并没有真正按照我想要的方式返回结果:
select id, level, count(parent) as p, country
from stats group by level order by level asc, p desc;
你能帮我吗?
表数据
"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"
期望的输出
"id" "type" "country" "Count"
"1" "1" "US" "4"
"8" "1" "US" "3"
"10" "2" "US" "4"
"9" "2" "US" "3"
"2" "2" "US" "2"