Search
 
SCRIPT & CODE EXAMPLE
 

SQL

parent child hierarchy in sql

WITH RECURSIVE generation AS (
    SELECT id,
        first_name,
        last_name,
        parent_id,
        0 AS generation_number
    FROM parent_child
    WHERE parent_id IS NULL
 
UNION ALL
 
    SELECT child.id,
        child.first_name,
        child.last_name,
        child.parent_id,
        generation_number+1 AS generation_number
    FROM parent_child child
    JOIN generation g
      ON g.id = child.parent_id
)
 
SELECT first_name,
     last_name,
     generation_number
FROM generation;
Comment

PREVIOUS NEXT
Code Example
Sql :: sql select maximum column with other columns returned 
Sql :: sql like with multiple values 
Sql :: sql to excel pgadmin 
Sql :: mysql select random rows large table 
Sql :: mamp mysql password 
Sql :: drop table oracle 
Sql :: sql server get week dates from week number 
Sql :: mariadb check constraint example? 
Sql :: how use trigger in sql 
Sql :: what is table in sql 
Sql :: inspecting a column unique/distinct values in SQL 
Sql :: not null sql 
Sql :: t sql return on letters only 
Sql :: install sql server in ubuntu 20.04 
Sql :: while mysql 
Sql :: SQL JOIN and Aliases 
Sql :: error code 1241 mysql 
Sql :: group by sql 
Sql :: test connection to sql server 
Sql :: is firebase nosql 
Sql :: SQL FROM-Klausel 
Sql :: sql query to linq converter online 
Sql :: pl sql revoke role from user 
Sql :: INSERT INTO GBP Plus(Index Change) VALUES( AND((SELECT NUMINDEX FROM GBP WHERE IDID-1) - (SELECT NUMINDEX FROM GBP WHERE ID=ID )) 
Sql :: like sql for second letter of the family name 
Sql :: sql tablo hangi sp de 
Sql :: sql_inner_join 
Sql :: sql create text column limited values 
Sql :: copy data from cell to cell mysql 
Sql :: mssql get running queries by spid 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =