> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-fix-nav-issues.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> 入力パラメータと同じデータ型で結果を返し、数値の合計を計算します。合計がこのデータ型の最大値を超える場合は、オーバーフローしたまま計算されます。

# sumWithOverflow

<div id="sumWithOverflow">
  ## sumWithOverflow
</div>

導入バージョン: v1.1.0

数値の合計を計算します。結果には、入力パラメータと同じデータ型が使用されます。
合計がこのデータ型の最大値を超える場合は、オーバーフローした状態で計算されます。

**構文**

```sql theme={null}
sumWithOverflow(num)
```

**引数**

* `num` — 数値型の値を持つカラム。[`(U)Int*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal)

**戻り値**

値の合計。[`(U)Int*`](/ja/reference/data-types/int-uint) または [`Float*`](/ja/reference/data-types/float) または [`Decimal*`](/ja/reference/data-types/decimal)

**例**

**UInt16 でのオーバーフローの挙動**

```sql title=Query theme={null}
CREATE TABLE employees
(
    id UInt32,
    name String,
    monthly_salary UInt16 -- selected so that the sum of values produces an overflow
)
ENGINE = Memory;

INSERT INTO employees VALUES
    (1, 'John', 20000),
    (2, 'Jane', 18000),
    (3, 'Bob', 12000),
    (4, 'Alice', 10000),
    (5, 'Charlie', 8000);

-- sum関数とsumWithOverflow関数を使用して従業員の給与合計を求め、toTypeName関数でその型を表示するクエリ
-- sum関数の結果型はUInt64であり合計値を格納するのに十分な大きさだが、sumWithOverflowの結果型はUInt16のままになる。

SELECT
    sum(monthly_salary) AS no_overflow,
    sumWithOverflow(monthly_salary) AS overflow,
    toTypeName(no_overflow),
    toTypeName(overflow)
FROM employees;
```

```response title=Response theme={null}
┌─no_overflow─┬─overflow─┬─toTypeName(no_overflow)─┬─toTypeName(overflow)─┐
│       68000 │     2464 │ UInt64                  │ UInt16               │
└─────────────┴──────────┴─────────────────────────┴──────────────────────┘
```
