# 集計ファンクション

集計ファンクションは通常GROUP BY句で使われますが、標準的なFROM-WHERE-SELECT句や、GROUP BYコンテキストにおける条件式の中で使用することもできます。

## 集計ファンクションを使用する場面

集計ファンクションは反復コンテキストで動作し、複数の行またはエレメントにまたがる値を処理します。GROUP BY句を含むクエリを使用する場合や、FROM-SELECTクエリでデータセットの値を集計する場合に集計ファンクションを使用します。

たとえば、すべての顧客レコードにおける平均評価を計算する場合などです。

```
FROM customer
SELECT avg(rating)
```

式がGROUP BYコンテキストの範囲内にある場合は、条件内で集計ファンクションを使用することもできます。例:

```
FROM order
GROUP BY custid AS cid
SELECT {
  customer_id = cid,
  order_status = if (count(orderno) == 1) 'One order' ELSE IF (count(orderno) == 2) 'Two orders' ELSE 'At least three orders'
}
ORDER BY cid
```

条件式における集計ファンクションは、GROUP BYコンテキストの外部ではサポートされていません。たとえば、次のスクリプトはサポートされていません。

```
FROM customer
WHERE hasValue(rating)
SELECT IF (avg(rating) > 500) 'Good customers' ELSE 'Bad customers'
```

集計ファンクションと配列ファンクションの違いについて: 集計ファンクションは反復的なコンテキストで値を処理するのに対し、配列ファンクションは単一のコレクション値に対して動作します。配列操作については、[配列ファンクション](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/array-functions)を参照してください。

## 追加の集計ファンクション

次のファンクションも集計ファンクションとして使用できます。

* [concat](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/concat-function)
* [concatWith](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/concatwith-function)
* [min](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/min-function)
* [max](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/max-function)

### このセクション内

* [avg](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/avg-function)
* [count](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/count-function)
* [first](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/first-function)
* [last](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/last-function)
* [sum](https://help.qlik.com/talend/ja-JP/data-shaping-language-reference-guide/8.0/sum-function)