I have a task to transform a table that has no hierarchy into a view of the table where it will be added two columns (id and parent) this is for establishing parent-child hierarchy and using it to build a visualization of a tree. The data is regarding the classification of tumors made by the WHO organization called ICD 10. In my table, there are no categories yet and my task is to create categories and then add the relation pattern following the classification made by WHO
- C00-C97 :
C00-C75
C76-C80
C81-C96
C97-C97
- D00-D09
- D10-D36
- D37-D48
The task is to investigate the feasibility through the use of views otherwise we will alter the original table.
An example of a table:
Uri | Label | timestamp |
---|---|---|
cim10:c00 | text2 | text54 |
cim10:c01 | text3 | text55 |
cim10:c02 | text4 | text56 |
... | ... | ... |
cim10:c97 | text99 | text151 |
cim10:d00 | text100 | text152 |
cim10:d01 | text101 | text153 |
I have tried something like this (don't mind the categories as this is the original classification):
CREATE VIEW diagnosis_cim10_tumors_view AS
SELECT *
FROM diagnosis
WHERE Uri LIKE 'cim10:C%'
OR Uri LIKE 'cim10:D0%'
OR Uri LIKE 'cim10:D[1-3][0-9]%'
OR Uri LIKE 'cim10:D4[0-8]%'
OR Uri LIKE 'cim10:D49%';
Result I want to achieve, note how the 'child2' Uri was not originally present in the table and has to be added via the query (cim10:D00-D09) . root refers to "classification" label which represent the starting point:
Uri | id | parent |
---|---|---|
cim10:D00-D09 | child2 | root |
cim10:D00 | child2.1 | child2 |
cim10:D01 | child2.2 | child2 |
cim10:D02 | child2.3 | child2 |
cim10:D03 | child2.4 | child2 |
cim10:D04 | child2.5 | child2 |
cim10:D05 | child2.6 | child2 |
cim10:D06 | child2.7 | child2 |
cim10:D07 | child2.8 | child2 |
cim10:D09 | child2.9 | child2 |
Answers
To achieve the desired result, you can use a recursive Common Table Expression (CTE) in your view definition. Here's how you can create the view:
CREATE VIEW diagnosis_cim10_tumors_view AS
WITH RECURSIVE TumorHierarchy AS (
-- Anchor member: Select the root nodes
SELECT
Uri,
'root' AS id,
NULL AS parent
FROM
diagnosis
WHERE
Uri LIKE 'cim10:C%' OR
Uri LIKE 'cim10:D0%'
UNION ALL
-- Recursive member: Select the child nodes
SELECT
d.Uri,
CONCAT('child', ROW_NUMBER() OVER (PARTITION BY p.id ORDER BY d.Uri)) AS id,
p.id AS parent
FROM
diagnosis d
JOIN
TumorHierarchy p ON d.Uri LIKE CONCAT(p.Uri, '%')
)
SELECT * FROM TumorHierarchy;
In this query:
- We use a recursive CTE named
TumorHierarchy
to traverse the tumor classification hierarchy. - In the anchor member, we select the root nodes by filtering based on the starting patterns 'cim10:C%' and 'cim10:D0%'.
- In the recursive member, we join the CTE
TumorHierarchy
with thediagnosis
table to find the child nodes based on the parent nodes selected in the previous iteration. - We use the
ROW_NUMBER()
window function to generate unique IDs for the child nodes within each parent group. - Finally, we select all columns from the recursive CTE
TumorHierarchy
to create the view.
This view will recursively generate the parent-child hierarchy for the tumor classification, starting from the root nodes ('cim10:C%' and 'cim10:D0%'). The id
column represents the unique identifier for each node, and the parent
column represents the parent node of each child node.