pg.ddx.io  pgsql-docs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: nnj@riseup.net
To: pgsql-docs@postgresql.org
Subject: I notice Postgresql 'HAVING' doesn't work.
Date: Thu, 05 Jan 2017 01:09:04 +0000
Message-ID: <20170105010904.10139.25730@wrigleys.postgresql.org> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-docs>

The following documentation comment has been logged on the website:

Page: https://www.postgresql.org/docs/9.5/static/tutorial-agg.html
Description:

Following this tutorial The Nested Set Model on
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
  

Section: Depth of a Sub-Tree.
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
        nested_category AS parent,
        nested_category AS sub_parent,
        (
                SELECT node.name, (COUNT(parent.name) - 1) AS depth
                FROM nested_category AS node,
                nested_category AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.name = &#39;PORTABLE ELECTRONICS&#39;
                GROUP BY node.name, node.lft
                ORDER BY node.lft
        )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
        AND sub_parent.name = sub_tree.name
GROUP BY node.name, node.lft, sub_tree.depth
ORDER BY node.lft;
+----------------------+---------+
| name                 |   depth |
|----------------------+---------|
| PORTABLE ELECTRONICS |       0 |
| MP3 PLAYERS          |       1 |
| FLASH                |       2 |
| CD PLAYERS           |       1 |
| 2 WAY RADIOS         |       1 |
+----------------------+---------+


Section: Find the Immediate Subordinates of a Node.
SELECT node.name, (COUNT(parent.name) - (sub_tree.depth + 1)) AS depth
FROM nested_category AS node,
        nested_category AS parent,
        nested_category AS sub_parent,
        (
                SELECT node.name, (COUNT(parent.name) - 1) AS depth
                FROM nested_category AS node,
                nested_category AS parent
                WHERE node.lft BETWEEN parent.lft AND parent.rgt
                AND node.name = &#39;PORTABLE ELECTRONICS&#39;
                GROUP BY node.name, node.lft
                ORDER BY node.lft
        )AS sub_tree
WHERE node.lft BETWEEN parent.lft AND parent.rgt
        AND node.lft BETWEEN sub_parent.lft AND sub_parent.rgt
        AND sub_parent.name = sub_tree.name
GROUP BY node.name, node.lft, sub_tree.depth
HAVING depth &lt;= 1
ORDER BY node.lft;
Adding &#39;HAVING depth &lt;= 1&#39; to the query still return the same results as
above instead of this:
+----------------------+---------+
| name                 |   depth |
|----------------------+---------|
| PORTABLE ELECTRONICS |       0 |
| MP3 PLAYERS          |       1 |
| FLASH                |       1 |
| CD PLAYERS           |       1 |
| 2 WAY RADIOS         |       1 |
+----------------------+---------+


NOTE: I edited the query by add node.lft, sub_tree.depth to the GROUP BY.

I don&#39;t know if I&#39;m doing anything wrong?


-- 
Sent via pgsql-docs mailing list (pgsql-docs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-docs


Message-ID: <20170105010904.10139.25730@wrigleys.postgresql.org>
Permalink:  ../20170105010904.10139.25730@wrigleys.postgresql.org/
Also on:    postgresql.org/message-id/20170105010904.10139.25730@wrigleys.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-docs@postgresql.org
  Cc: nnj@riseup.net
  Subject: Re: I notice Postgresql 'HAVING' doesn't work.
  In-Reply-To: <20170105010904.10139.25730@wrigleys.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox