-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathfacet_to_catalog.sql
50 lines (46 loc) · 1006 Bytes
/
facet_to_catalog.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- Step 1: Create the new table
CREATE TABLE resto.catalog_test (
id TEXT,
description TEXT
);
-- Step 2 & 3: Recursive query and insert into new table
WITH RECURSIVE cte AS (
-- Anchor member: Select the root nodes
SELECT
id AS original_id,
id AS newid,
id AS hashtag,
description,
pid
FROM
resto.facet
WHERE
pid = 'root'
UNION ALL
-- Recursive member: Join with child nodes
SELECT
child.id AS original_id,
CONCAT(cte.newid, '/', child.id) AS newid,
child.id as hashtag,
child.description,
child.pid
FROM
resto.facet AS child
INNER JOIN cte ON cte.original_id = child.pid
)
-- Insert the final results into the new table
-- INSERT INTO resto.catalog_test (id, description)
SELECT
newid,
title,
description,
level,
counters,
owner,
now() as created,
links,
visibility,
rtype,
hashtag
FROM
cte;