# Aggregate into ARRAY\<STRUCT\<...\>\>

**URL:** <https://forum.confluent.io/t/aggregate-into-array-struct/8879>\
**Category:** ksqlDB\
**Created:** [16 October 2023 14:00 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879 "2023-10-16T14:00:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![aherring](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@aherring](https://forum.confluent.io/u/aherring)\
**Post date:** [16 October 2023 14:00 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/1 "2023-10-16T14:00:33Z")

</div>

I am attempting to aggregate data into a structure similar to

```sql
-- Starting
CustomerId INTEGER,
ContactId STRING, 
Name STRING, 
Phone STRING

-- Desired Result
CustomerId INTEGER,
Contacts ARRAY< STRUCT< ContactId STRING, Name STRING, Phone STRING > >

```

When using `TOPK` to `GROUP BY` I am left with a struct of `sort_col, col0, col1, etc.`, so I am trying to `TRANSFORM` that result, but it is unable to find the columns defined by `TOPK`:

```sql
CREATE TABLE TEMP_CONTACTS
AS SELECT
  CustomerId,
  TOPK(
    ContactId, 
    Name,
    Phone,
    10) AS Contacts
FROM CONTACTS
GROUP BY CustomerId
EMIT CHANGES;

SELECT
  CustomerId,
  TRANSFORM(Contacts, (contact) => STRUCT(ContactId := contact->sort_col)) AS Contacts
FROM TEMP_CONTACTS EMIT CHANGES;
-- > Could not find field 'SORT_COL' in 'CONTACT'.

```

Is there a better way to accomplish this aggregation, or something I am missing with the transformation? I had tried to convert it to an single `STRUCT` and using other aggregate functions on that, but the other aggregate functions did not accept a `STRUCT`.

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [16 October 2023 17:20 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/2 "2023-10-16T17:20:39Z")

</div>

> Contacts ARRAY\< STRUCT\< ContactId STRING, Name STRING, Phone STRING \> \>

It seems you define `Contracts` with three fields, `ContradId`, `Name`, and `Phone` but not with `sort_col`. So it seems clear why `sort_col` is not found.

---

<div class="post-metadata">

**Author:** ![aherring](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@aherring](https://forum.confluent.io/u/aherring)\
**Post date:** [16 October 2023 17:31 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/3 "2023-10-16T17:31:35Z")

</div>

TOPK defaults to `STRUCT<sort_col, col0, col1, etc.>` It doesn’t take the name provided and there is no way to override it from what I can tell.

> **[ksqlDB Aggregate Functions - ksqlDB Documentation](https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-reference/aggregate-functions/#topk)**
>
> Aggregate functions to use in SQL statements and queries

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [16 October 2023 18:54 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/4 "2023-10-16T18:54:03Z")

</div>

Did you try to use `contact->ContactId`?

---

<div class="post-metadata">

**Author:** ![aherring](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@aherring](https://forum.confluent.io/u/aherring)\
**Post date:** [16 October 2023 19:04 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/5 "2023-10-16T19:04:15Z")

</div>

Yes, it gives the same error about not finding the field.

When using `DESCRIBE` I see the following part of the schema:

```json
        "name": "CONTACTS",
        "schema": {
          "type": "ARRAY",
          "fields": null,
          "memberSchema": {
            "type": "STRUCT",
            "fields": [
              {
                "name": "sort_col",
                "schema": {
                  "type": "INTEGER",
                  "fields": null,
                  "memberSchema": null
                }
              },
              {
                "name": "col0",
                "schema": {
                  "type": "STRING",
                  "fields": null,
                  "memberSchema": null
                }

```

---

<div class="post-metadata">

**Author:** ![mjsax](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/mjsax/32/3113_2.png) [@mjsax](https://forum.confluent.io/u/mjsax)\
**Post date:** [16 October 2023 21:46 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/6 "2023-10-16T21:46:03Z")

</div>

Hmmm… not sure from top of my head… maybe somebody else knows. Sorry.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [15 November 2023 14:01 UTC](https://forum.confluent.io/t/aggregate-into-array-struct/8879/7 "2023-11-15T14:01:01Z")

</div>

Closed on 15 November 2023 14:01 UTC

This topic was automatically closed after 30 days. New replies are no longer allowed.
