Categories: Snippets

WITH RECURSIVE

WITH RECURSIVE children AS
  ( SELECT '0'::varchar AS parent_code,
           A.level_code,
           A.level_name,
           A.code_length,
           1 AS DEPTH
   FROM c_levels A
   WHERE A.level_code ='05'
   UNION ALL SELECT B.parent_code AS parent_code,
                    B.level_code,
                    B.level_name,
                    B.code_length,
                    DEPTH + 1
   FROM c_levels B
   JOIN children C ON ( B.level_code = C.parent_code ))
SELECT *
FROM children
ORDER BY DEPTH DESC

 

Recent Posts

Generate Slug URL in MySQL

A slug is a short name using human-readable keywords to identify a web page. For…

5 years ago

How to use SQL LIKE condition with multiple values in PostgreSQL?

[crayon-6639da03185c8309897769/]  

6 years ago

SELECT Null

[crayon-6639da03186e1530087536/]  

6 years ago

ALTER Column SET NOT NULL – pgSQL

[crayon-6639da0318802334079999/]  

6 years ago

ADD new Column – pgSQL

[crayon-6639da0318932556324153/] [crayon-6639da031893a874446223/]  

6 years ago