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…

6 years ago

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

[crayon-67a359513b3d2311388346/]  

6 years ago

SELECT Null

[crayon-67a359513b539124982541/]  

6 years ago

ALTER Column SET NOT NULL – pgSQL

[crayon-67a359513b63e460124295/]  

6 years ago

ADD new Column – pgSQL

[crayon-67a359513b883895920040/] [crayon-67a359513b887335804619/]  

6 years ago