DEV Community

Cover image for Calculate the Hierarchy of Recursive References — From SQL to SPL #11
Judith-Data-Processing-Hacks
Judith-Data-Processing-Hacks

Posted on

1 1 1 1 1

Calculate the Hierarchy of Recursive References — From SQL to SPL #11

Problem description & analysis:

A certain table in the MS SQL database has a multi-level self-association structure, where the second field parent node ID is a foreign key pointing to the first field node ID of the table, and the third field is a zone.

source table

Task: Now we need to find the hierarchy of nodes with a higher level of 2 or more, as well as the zones of the highest-level nodes. For example, the first record has 3 levels of hierarchy, which are 5–11–15, and the highest level is 15; The second record has two levels of superiors, namely 6–12, with the highest level being 12.

expected results

Code comparisons:

SQL solution

WITH dt AS (
  SELECT
    temp.product_identifier,
    temp.parent_product_identifier,
    temp.Zone,
    1 AS hierarchy,
    parent_product_identifier AS current_parent 
  FROM temp
  UNION ALL
  SELECT
     dt.product_identifier, 
     dt.parent_product_identifier, 
     temp.Zone, 
     dt.hierarchy+1, 
     temp.parent_product_identifier AS current_parent
  FROM dt
     INNER JOIN temp
     ON temp.product_identifier = dt.current_parent
)
SELECT 
  product_identifier,
  parent_product_identifier,
  hierarchy,
  Zone
FROM dt
WHERE hierarchy > 1 
AND hierarchy = (
   SELECT MAX(hierarchy) FROM dt dt2 
   WHERE dt2.product_identifier = dt.product_identifier) 
ORDER BY product_identifier;
WITH dt AS (
  SELECT
    temp.product_identifier,
    temp.parent_product_identifier,
    temp.Zone,
    1 AS hierarchy,
    parent_product_identifier AS current_parent 
  FROM temp
  UNION ALL
  SELECT
     dt.product_identifier, 
     dt.parent_product_identifier, 
     temp.Zone, 
     dt.hierarchy+1, 
     temp.parent_product_identifier AS current_parent
  FROM dt
     INNER JOIN temp
     ON temp.product_identifier = dt.current_parent
)
SELECT 
  product_identifier,
  parent_product_identifier,
  hierarchy,
  Zone
FROM dt
WHERE hierarchy > 1 
AND hierarchy = (
   SELECT MAX(hierarchy) FROM dt dt2 
   WHERE dt2.product_identifier = dt.product_identifier) 
ORDER BY product_identifier;
Enter fullscreen mode Exit fullscreen mode

As long as all the levels recursively referenced by each node are found, the results can be easily filtered out. However, SQL does not have directly available functions, and complex recursive subqueries plus self joins need to be used to implement them. The code is lengthy and difficult to understand.

SPL solution:

SPL provides directly available functions that can obtain all levels of recursive references to nodes. 👉🏻 Try.DEMO

SPL code

A2: Establish a reference relationship and replace parent_product_identifier with the record of this table pointed to by the foreign key.

A3: Add a calculated column and use the prior function to calculate all levels recursively referenced by this node, the number of levels in these levels, and the zone of the last level.

A4: Select nodes with levels greater than or equal to 2 in all levels of recursive references.

A5: Generate the target result set.


Download esProc SPL for FREE and give it a try👉🏻: esProc SPL FREE Download

Hot sauce if you're wrong - web dev trivia for staff engineers

Hot sauce if you're wrong · web dev trivia for staff engineers (Chris vs Jeremy, Leet Heat S1.E4)

  • Shipping Fast: Test your knowledge of deployment strategies and techniques
  • Authentication: Prove you know your OAuth from your JWT
  • CSS: Demonstrate your styling expertise under pressure
  • Acronyms: Decode the alphabet soup of web development
  • Accessibility: Show your commitment to building for everyone

Contestants must answer rapid-fire questions across the full stack of modern web development. Get it right, earn points. Get it wrong? The spice level goes up!

Watch Video 🌶️🔥

Top comments (1)

Collapse
 
judith677 profile image
Judith-Data-Processing-Hacks

Download esProc SPL and share your feedback with us!

Discord
Reddit

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay