This query:
SELECT pcn.id,
pcn.config_id,
pcnc.nombre_nivel,
pcnc.orden_nivel,
pcn.nivel_padre_id,
pcnc.empresa_id,
pcnc.proyecto_id,
pcnc.activo AS activo_config,
pcnc.usuario_id,
pcn.activo
FROM puebles_ciclos_niveles pcn
JOIN puebles_ciclos_niveles_config pcnc ON pcn.config_id = pcnc.id
Provides these results:
The order I need for this data is the following:
In here, "nivel_padre_id" represents the parent ID.
I've tried making it with a CTE table, the problem is that the CTE table only adresses one level like so:
WITH RecursiveCTE AS (
SELECT
pcn.id, pcn.config_id, pcnc.nombre_nivel, pcnc.orden_nivel,pcn.nivel_padre_id,
pcnc.empresa_id, pcnc.proyecto_id, pcnc.activo AS activo_config, pcnc.usuario_id,pcn.activo
FROM
puebles_ciclos_niveles pcn
JOIN
puebles_ciclos_niveles_config pcnc ON pcn.config_id = pcnc.id
WHERE
pcn.nivel_padre_id = 0 -- Selecting the root level
UNION ALL
SELECT
pcn.id, pcn.config_id, pcnc.nombre_nivel, pcnc.orden_nivel, pcn.nivel_padre_id, pcnc.empresa_id, pcnc.proyecto_id,
pcnc.activo AS activo_config, pcnc.usuario_id, pcn.activo
FROM
puebles_ciclos_niveles pcn
JOIN
puebles_ciclos_niveles_config pcnc ON pcn.config_id = pcnc.id
JOIN
RecursiveCTE rc ON pcn.nivel_padre_id = rc.id
)
SELECT
id, config_id, nombre_nivel, orden_nivel,
nivel_padre_id, empresa_id, proyecto_id,
activo_config, usuario_id, activo
FROM
RecursiveCTE
This CTE query produces the following:
Is there any way to get the desired result? (Parent - Child(1) - Child (2) - Child(3) - Children (4) -Stop if no more children are found, go back to process the previous level-)



Because you want a specific ordering in your result set, you need to provide an
order byclause. But what to order it by? Here's your query with the addition of one column -pathwhich represents the path from the root node to that particular row.By way of exposition, the
pathfor the base element is just that row'sid, surrounded by slashes. For subsequent/recursive elements, I take the parent's path, append this row'sidto it, and end it with another slash. Here's a db fiddle with a demonstration.Note - the choice of formatting for the path is not arbitrary. SQL Server has a data type called hierarchyid that you can use to get this sort of behavior much less expensively than calculating it dynamically at query time (with the overhead of needing to maintain it on data mutation). The
pathformat that I used is directly castable tohierarchyidshould you choose to use it.