Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按PostgreSQL Ltree的最大深度分组的查询?_Sql_Database_Postgresql_Group By - Fatal编程技术网

按PostgreSQL Ltree的最大深度分组的查询?

按PostgreSQL Ltree的最大深度分组的查询?,sql,database,postgresql,group-by,Sql,Database,Postgresql,Group By,我想查询所有名为“Shania Twain”的产品,但我只想按最深的树对它们进行分组 假设我有一个名为categories +---------------+---------------+---------------+ |id |name |tree | +---------------+---------------+---------------+ |1 |"Music" |100

我想查询所有名为“Shania Twain”的产品,但我只想按最深的树对它们进行分组

假设我有一个名为
categories

+---------------+---------------+---------------+
|id             |name           |tree           |
+---------------+---------------+---------------+
|1              |"Music"        |100            |
+---------------+---------------+---------------+
|2              |"Shania Twain" |100.1          |
+---------------+---------------+---------------+
|3              |"Artists"      |200            |
+---------------+---------------+---------------+
|5              |"Country"      |200.2          |
+---------------+---------------+---------------+
|6              |"Shania Twain" |200.2.4        |
+---------------+---------------+---------------+
那么比如说,

SELECT MAX(cat.id),
       cat.name,
       MAX(cat.tree)
  FROM
       public.categories cat
 WHERE
       cat.name = "Shania Twain"
GROUP BY
       name
HAVING
       MAX(nlevel(cat.tree))
问题在于
具有需要布尔表达式的
子句。子句
MAX(nlevel(cat.tree))
将返回一个整数

我该怎么做呢

提前谢谢


马哈茂德

我会接受这个答案,因为它回答了我的问题,但我想看看我会如何处理所有NL级别最深的条目。
SELECT
    cat.id
    cat.name,
    cat.tree
FROM
    public.categories AS cat
WHERE
    cat.name = 'Shania Twain'
AND
   NLEVEL(cat.tree) = (SELECT MAX(NLEVEL(tree) FROM public.categories WHERE cat.name='Shania Twain'))