By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE
INSERT 0 3
INSERT 0 10
| id | code | type | parentId |
|---|---|---|---|
| 28 | acc03.001 | type03 | null |
| 29 | acc03.001.001 | type03 | 28 |
| 30 | acc03.001.002 | type03 | 28 |
| 10 | bcc03.001 | type04 | null |
| 20 | bcc03.001.001 | type04 | 10 |
| 21 | bcc03.001.002 | type04 | 10 |
| 33 | bcc03.001.001.001 | type04 | 20 |
| 31 | bcc03.001.001.002 | type04 | 20 |
| 25 | bcc03.001.002.001 | type04 | 21 |
| 40 | bcc03.001.001.001.001 | type04 | 33 |
| 41 | bcc03.001.001.001.002 | type04 | 33 |
| 44 | xcc03.001 | type04 | null |
| 66 | ycc03.001 | type06 | null |
SELECT 13
CREATE FUNCTION
| jsonb_pretty |
|---|
| [ { "type": "type03", "accounts": [ { "id": 28, "code": "acc03.001", "type": "type03", "children": [ { "id": 29, "code": "acc03.001.001", "type": "type03", "parentId": 28 }, { "id": 30, "code": "acc03.001.002", "type": "type03", "parentId": 28 } ], "parentId": null } ] } ] |
SELECT 1
| jsonb_pretty |
|---|
| [ { "type": "type03", "accounts": [ { "id": 28, "code": "acc03.001", "type": "type03", "children": [ { "id": 29, "code": "acc03.001.001", "type": "type03", "children": null, "parentId": 28 }, { "id": 30, "code": "acc03.001.002", "type": "type03", "children": null, "parentId": 28 } ], "parentId": null } ] }, { "type": "type04", "accounts": [ { "id": 10, "code": "bcc03.001", "type": "type04", "children": [ { "id": 20, "code": "bcc03.001.001", "type": "type04", "children": [ { "id": 31, "code": "bcc03.001.001.002", "type": "type04", "children": null, "parentId": 20 }, { "id": 33, "code": "bcc03.001.001.001", "type": "type04", "children": [ { "id": 40, "code": "bcc03.001.001.001.001", "type": "type04", "parentId": 33 }, { "id": 41, "code": "bcc03.001.001.001.002", "type": "type04", "parentId": 33 } ], "parentId": 20 } ], "parentId": 10 }, { "id": 21, "code": "bcc03.001.002", "type": "type04", "children": [ { "id": 25, "code": "bcc03.001.002.001", "type": "type04", "children": null, "parentId": 21 } ], "parentId": 10 } ], "parentId": null }, { "id": 44, "code": "xcc03.001", "type": "type04", "children": null, "parentId": null } ] }, { "type": "type06", "accounts": [ { "id": 66, "code": "ycc03.001", "type": "type06", "children": null, "parentId": null } ] } ] |
SELECT 1