# UDAF: What is state type used for?

**URL:** <https://www.singlestore.com/forum/t/udaf-what-is-state-type-used-for/6613>\
**Category:** Procedural SQL\
**Tags:** community\
**Created:** [March 14, 2023, 8:14am UTC](https://www.singlestore.com/forum/t/udaf-what-is-state-type-used-for/6613 "2023-03-14T08:14:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![michal.kyjovsky](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/m/c5a1d2/32.png) [@michal.kyjovsky](https://www.singlestore.com/forum/u/michal.kyjovsky)\
**Post date:** [March 14, 2023, 8:14am UTC](https://www.singlestore.com/forum/t/udaf-what-is-state-type-used-for/6613/1 "2023-03-14T08:14:45Z")

</div>

Hello community,

I am reading the documentation on the UDAF topic and struggling to understand the state type used. It is being referenced throughout the documentation and is always referenced by the following string _The `STATE` type of a UDAF may be a scalar type, `ARRAY` type, or a `RECORD` type._

Can someone provide more information?

Thank you all!

Have a great day!

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [March 14, 2023, 8:15pm UTC](https://www.singlestore.com/forum/t/udaf-what-is-state-type-used-for/6613/2 "2023-03-14T20:15:55Z")

</div>

The STATE is just the summary of the information needed to compute the aggregate, for some set of rows. For example, for an average aggregate, you might have this as your state:

`record(count_of_rows bigint, sum_of_values bigint)`

At the end, when you are asked to get the aggregate result, you will return it from the STATE. Also, you can merge two STATE values together by summing the count\_of\_rows and sum\_of\_values together from the two different states.

The example [here](https://docs.singlestore.com/managed-service/en/reference/sql-reference/procedural-sql-reference/create-aggregate.html#example-655173) has a state `record(s bigint, c bigint)` similar to what I described.
