# How to aggregate Children to Parents

**URL:** <https://community.cratedb.com/t/how-to-aggregate-children-to-parents/1650>\
**Category:** SQL\
**Created:** [November 21, 2023, 2:15pm UTC](https://community.cratedb.com/t/how-to-aggregate-children-to-parents/1650 "2023-11-21T14:15:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![surister](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/surister/32/1087_2.png) [@surister](https://community.cratedb.com/u/surister)\
**Post date:** [November 21, 2023, 2:15pm UTC](https://community.cratedb.com/t/how-to-aggregate-children-to-parents/1650/1 "2023-11-21T14:15:31Z")

</div>

Hi, I’m trying to aggregate parent → children tables,

for example we could understand that rows from `sys.Snapshot` are children of `sys.Repositories`.

Ideally I’m trying to achieve something like:

`repositories: [...[repo1, snapshots: [snapshot1, snapshot2]], ]`

```auto
[
   [
     "myrepo1", {“location”:“/data/backup”}, "fs", [
        {"state": "Successful", "name": "snap1"}, 
        {"state": "Successful", "name": "snap2"}
       ] 
   ],
   [
     "myrepo2": {“location”:“/data/backup/repo2”}, "fs", []
   ]
]

```

Trying different stuff this is the closest thing I’d get:

##### Query ran at 2023-11-21T13:57:39.498Z on CrateDB 5.5.0

```auto
SELECT
  repo.*,
  (
    SELECT
      array_agg(snap.name) -- <-- Can only be given one parameter
    FROM
      sys.snapshots snap
    WHERE
      repo.name = snap.repository
  ) as snapshots
FROM
  sys.repositories repo

```

##### QUERY OK, 2 record(s) returned in 0.0016s

| name | settings | type | snapshots |
| --- | --- | --- | --- |
| myrepo2 | {“location”:“/data/backup/repo2”} | fs | |
| myrepo1 | {“location”:“/data/backup”} | fs | snap2,snap1 |

Any tips on how I could achieve this?

---

<div class="post-metadata">

**Author:** ![proddata](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/proddata/32/1379_2.png) [@proddata](https://community.cratedb.com/u/proddata)\
**Post date:** [November 21, 2023, 2:34pm UTC](https://community.cratedb.com/t/how-to-aggregate-children-to-parents/1650/2 "2023-11-21T14:34:31Z")

</div>

Not quite sure what exactly you are trying to do, but you could …

use a `JOIN`

```sql
WITH snap_repos AS (
  SELECT r.*, s.*
  FROM sys.repositories r
  LEFT JOIN sys.snapshots s ON r.name = s.repository
)
SELECT *
FROM snap_repos

```

and/or us `OJBECTS` in `array_agg`

```sql
-- ...
SELECT
      array_agg({name = snap.name, tables = snap.tables}) -- <-- Can only be given one parameter
-- ...

```

---

<div class="post-metadata">

**Author:** ![proddata](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/proddata/32/1379_2.png) [@proddata](https://community.cratedb.com/u/proddata)\
**Post date:** [November 21, 2023, 2:44pm UTC](https://community.cratedb.com/t/how-to-aggregate-children-to-parents/1650/3 "2023-11-21T14:44:22Z")

</div>

Maybe something like this?

```sql
WITH snaps AS (
  SELECT
    "repository",
    ARRAY_AGG({"state" = "state", "name" = "name"}) snapshots
  FROM sys.snapshots
  GROUP BY "repository"
), repos AS (
  SELECT
    "name", 
    "type",
    "settings"
  FROM sys.repositories
)
SELECT
  ARRAY_AGG({
    "name" = "name",
    "type" = "type",
    "settings" = "settings",
    "snapshots" = "snapshots"
    }) as repos
FROM repos
LEFT JOIN snaps
ON repos."name" = snaps."repository";

```

---

<div class="post-metadata">

**Author:** ![surister](https://sea2.discourse-cdn.com/flex020/user_avatar/community.cratedb.com/surister/32/1087_2.png) [@surister](https://community.cratedb.com/u/surister)\
**Post date:** [November 21, 2023, 3:07pm UTC](https://community.cratedb.com/t/how-to-aggregate-children-to-parents/1650/4 "2023-11-21T15:07:44Z")

</div>

Thank you very much, with your examples I was able to get what I want, I didn’t know `array_agg` could be used like that, I couldn’t find many examples of it.

```auto
SELECT
  repo.*,
  (
    SELECT 
      ARRAY_AGG({
        "name" = snaps.name,
        "concrete_indices" = snaps.concrete_indices
         -- ... more fields
      })
    FROM
      sys.snapshots snaps
    WHERE
      repo.name = snaps.repository
  ) as snapshots
FROM
  sys.repositories repo

```

I wanted every row to be a repository, and inside every row a ‘snapshots’ array field with every snapshot object

 ![image](https://us1.discourse-cdn.com/flex020/uploads/crate/original/2X/5/5a1349d8e516cdcfea07e3dcc98c29563b052b87.png)
