Would like feedback on my SQL (1) efficiency and (2) readability, and if the conditions I featured in my CASE statement could be transferred to a join, etc.
Here's the problem I'm solving:
Say you have a table
bstwith a column of nodes and column or corresponding parent nodes:node Parent 1 2 2 5 3 5 4 3 5 NULLWe want to write SQL such that we label each node as a “leaf”, “inner” or “Root” node, such that for the nodes above we get:
Node Label 1 Leaf 2 Inner 3 Inner 4 Leaf 5 Root
Here's my solution:
WITH join_table AS
(
SELECT
a.node a_node,
a.parent a_parent,
b.node b_node,
b.parent b_parent
FROM
bst a
LEFT JOIN
bst b ON a.parent = b.node
)
SELECT
a_node as Node,
CASE
WHEN b_node IS NOT NULL and b_parent IS NOT NULL THEN 'Leaf'
WHEN b_node IS NOT NULL and b_parent IS NULL THEN 'Inner'
WHEN b_node IS NULL and b_parent IS NULL THEN 'Root'
ELSE 'Error'
END AS Label
FROM
join_table
CASE, however, it will not help the performance since you are reading the whole tables anyway. The sequential scan is inevitable in this case. \$\endgroup\$UNION ALLwhich would lead to three sequential scans instead of one. \$\endgroup\$