PostgreSQL
 sql >> Teknologi Basis Data >  >> RDS >> PostgreSQL

Graf asiklik terarah:temukan semua jalur dari simpul tertentu

WITH RECURSIVE parent_line(path, id) AS (
    SELECT ARRAY[(row_number() OVER (PARTITION BY CP.idc))::integer], C.id
    FROM categorias C JOIN categ_parents CP ON C.id = CP.idparent
    WHERE CP.idc = 36
    UNION ALL
    SELECT PL.path || (row_number() OVER (PARTITION BY PL.id))::integer,
        C.id
    FROM categorias C
    JOIN categ_parents CP ON C.id = CP.idparent
    JOIN parent_line PL on CP.idc = PL.id
),
real_parent_line(path, chainid, id) AS (
    SELECT PL.path, (row_number() OVER (PARTITION BY PL.id)),
        PL.id
    FROM parent_line PL
    WHERE PL.id IN (
        SELECT id
        FROM categorias C LEFT JOIN categ_parents CP
            ON (C.id = CP.idc)
        WHERE CP.idc IS NULL
    )
    UNION ALL
    SELECT PL.path, chainid, PL.id
    FROM parent_line PL, real_parent_line RPL
    WHERE array_upper(PL.path,1) + 1 = array_upper(RPL.path,1)
        AND PL.path = RPL.path[1:(array_upper(RPL.path,1)-1)]
)
SELECT array_accum(id) AS ids
FROM real_parent_line RPL
GROUP BY chainid;

WITH pertama klausa memberikan ini:

path             | id
------------------------
"{1}"              31
"{1,1}"            22
"{1,2}"            30
"{1,1,1}"          20
"{1,1,2}"          8
"{1,2,1}"          20
"{1,1,2,1}"        5
"{1,1,1,1}"        1
"{1,2,1,2}"        1
"{1,1,2,1,1}"      2
"{1,1,2,1,1,1}"    1
"{1,1,2,1,1,2}"    20
"{1,1,2,1,1,2,1}"  1

Terima kasih kepada #[email protected] untuk beberapa bantuan.




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. DROP TABLE JIKA ADA Contoh di PostgreSQL

  2. Bagaimana memaksa evaluasi subquery sebelum bergabung / menekan ke server asing

  3. Operator tidak ada:integer =integer[] dalam kueri dengan APAPUN

  4. desain database postgresql untuk e-niaga

  5. Impor byte mentah sebagai byte mentah di R