Skip to content

Commit 3b114a1

Browse files
committed
Update MP SELECT Descendants.md
1 parent 0954e42 commit 3b114a1

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

MP SELECT Descendants.md

+34-24
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ permalink: /mat-paths/select-desc
88

99
### Scope of provided selection queries for the MPs model
1010

11-
This and the following sections provide a family of standard MPs queries for selecting a set of category tree nodes, their descendants, and ancestors. These queries return rows from the *categories* table for the following standard MPs node sets:
11+
Current and the following sections provide a family of standard MPs queries for selecting a set of category tree nodes, their descendants, and ancestors. These queries return rows from the *categories* table for the following standard MPs node sets:
1212

1313
- root nodes (just the nodes from the input)
14-
- all descendants, excluding the root nodes
1514
- subtrees, including root nodes and descendants
15+
- all descendants, excluding the root nodes
1616
- children, i.e., direct descendants only
1717

1818
All provided selection queries have the [modular CTEs-based](../meta/db-derived-cte) structure and take a unified input in the form of a [JSON-array-formatted](../patterns/json-sql-input) string
@@ -25,7 +25,7 @@ with a list of category *path* values, identifying subtree root nodes.
2525

2626
### Query structure and alternative category identifiers
2727

28-
Each query starts with a prologue. Its function is to take the unified JSON array string of category identifiers (the *json_nodes* entry block) and its exit *tops* block produces a table containing the *path* column with one category path per row:
28+
Each query starts with a prologue. Its function is to take a unified JSON array string with category identifiers (the *json_nodes* entry block), and its exit *tops* block produces a table containing the *path* column with one category path per row:
2929

3030
#### Prologue input
3131

@@ -58,15 +58,15 @@ SELECT * FROM tops ORDER BY path;
5858

5959
---
6060

61-
Query prologue decouples the input format details from the query business logic. With query prologue and its output format defined, the input format and query business logic can be modified indepently. For example, switching from path-based to ascii_id-based identifies requires a localized change to a single block *tops*, identical for all queries in this family (immediate input in *json_nodes* is also adjusted; in production, a query parameter replaces this string input):
61+
The query prologue decouples the input format details from the query business logic, permitting their independent modification. For example, switching from path-based to ascii_id-based identifies requires a localized change to a single block *tops*, identical for all queries in this family (immediate input in *json_nodes* is also adjusted; in production, a query parameter replaces this string input):
6262

63-
#### Input
63+
#### Input - *ascii_id*
6464

6565
~~~json
6666
["0FDAF2C8","65887f45"]
6767
~~~
6868

69-
#### Prologue
69+
#### Prologue - *ascii_id*
7070

7171
~~~sql
7272
WITH
@@ -78,19 +78,40 @@ WITH
7878
SELECT node_ids.value AS node_id
7979
FROM json_nodes AS jn, json_each(jn.ids) AS node_ids
8080
),
81-
tops AS (
82-
SELECT cats.path
83-
FROM categories AS cats, nodes
84-
WHERE cats.ascii_id IN (nodes.node_id)
85-
)
81+
tops AS (SELECT path FROM categories WHERE ascii_id IN nodes)
8682
SELECT * FROM tops ORDER BY path;
8783
~~~
8884

8985
---
9086

91-
### MPs queries
87+
### Selection queries
88+
89+
The following query selects just the nodes from the input (root nodes).
90+
91+
~~~sql
92+
WITH
93+
json_nodes(ids) AS (
94+
VALUES
95+
('["tcl/compat/zlib1/", "BAZ/dev/git4win/x32/mingw32/share/"]')
96+
),
97+
nodes AS (
98+
SELECT node_ids.value AS node_id
99+
FROM json_nodes AS jn, json_each(jn.ids) AS node_ids
100+
),
101+
tops AS (SELECT node_id AS path FROM nodes),
102+
records AS (SELECT * FROM categories WHERE path IN nodes)
103+
SELECT * FROM records ORDER BY path;
104+
~~~
105+
106+
The remaining three queries in this section have almost identical code, with the only difference in the WHERE clause (*records*). In the query template below, replace %SELECTOR% according to the following table:
92107

93-
Note that the four queries have alomst identical code, with the only difference in the WHERE clause (*records*). Here is a query template:
108+
<pre>
109+
| <b>node set</b> | <b>%SELECTOR%</b> |
110+
|-------------|-----------------------------------|
111+
| subtrees | cats.path like tops.path || '%' |
112+
| descendants | cats.prefix like tops.path || '%' |
113+
| children | cats.prefix = tops.path |
114+
</pre>
94115

95116
~~~sql
96117
WITH
@@ -110,14 +131,3 @@ WITH
110131
)
111132
SELECT * FROM records ORDER BY path;
112133
~~~
113-
114-
in which %SELECTOR% should be replaced according to the following table
115-
116-
<pre>
117-
| <b>node set</b> | <b>%SELECTOR%</b> |
118-
|-------------|------------------------------------|
119-
| root nodes | cats.path IN (tops.path) |
120-
| descendants | cats.prefix like tops.path || '%' |
121-
| subtrees | cats.path like tops.path || '%' |
122-
| children | cats.prefix = tops.path |
123-
</pre>

0 commit comments

Comments
 (0)