Ltree找到孩子最多的父母PostgreSQL [英] Ltree find parent with most children postgresql

查看:57
本文介绍了Ltree找到孩子最多的父母PostgreSQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找孩子最多的父母。我觉得这应该没那么难,但是我发现自己很挣扎。

I am trying to find the parent with the most children. I feel like this should not be so hard however I find myself struggling.

我正在使用postgresql和Ltree。

I am using postgresql and Ltree.

谢谢您

推荐答案

与其他情况一样,请找到一个可以为您提供父级和组级的函数通过它。这应该起作用:

It's the same logic as for any other case, find a function that will give you the parent and group by it. This should work:

SELECT subpath(path,0,1), count(*) 
FROM test
GROUP BY subpath(path,0,1)
ORDER BY count(*) DESC limit 1;

http://sqlfiddle.com/#!12/8fb17/5

这篇关于Ltree找到孩子最多的父母PostgreSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆