Avg 集計 Function (*.fnc)
構文
%%{init: {
'theme': 'base',
'flowchart': { 'padding': '7', 'nodeSpacing': '20', 'rankSpacing': '20' },
'themeVariables': {
'fontSize': '11px',
'fontFamily': 'Arial'
}
}}%%
flowchart TD
avgAggregateFunction_start((START))
avgAggregateFunction_start --> avgAggregateFunction_0_0[AVG]:::quoted
avgAggregateFunction_0_0 --> avgAggregateFunction_0_1["("]:::quoted
avgAggregateFunction_0_1 --> avgAggregateFunction_0_3
avgAggregateFunction_0_1 --> avgAggregateFunction_0_2[DISTINCT]:::quoted
avgAggregateFunction_0_1 --> avgAggregateFunction_0_2a[ALL]:::quoted
avgAggregateFunction_0_2a --> avgAggregateFunction_0_3
avgAggregateFunction_0_2 --> avgAggregateFunction_0_3[<a href="Invantive UniversalSQL/General/Identifiers" class="internal-link">identifier</a>]
avgAggregateFunction_0_3 --> avgAggregateFunction_0_4[")"]:::quoted
avgAggregateFunction_0_4 --> avgAggregateFunction_end((END))
目的
数値のグループから平均値を求めるグループ関数です。 nullの出現は無視されます。 DISTINCTを指定すると、各一意の値は出現回数に関係なく一度だけ平均の計算に使われます。 ALL(既定値)を指定すると、各値は出現した回数分だけ計上されます。ALLはリリース28.0から使用できます。
例
次の例では、5 社の顧客ベース全体の平均与信限度額を取得します:
select avg(cus.credit_limit)
from csvtable
( passing 'Acme Industries#Manufacturing#12500#50000|Bergmann GmbH#Manufacturing#0#25000|Carlson Ltd#Wholesale#2500#10000|Dubois SA#Wholesale#7500#10000|Everest BV#Services##5000'
row delimiter '|'
column delimiter '#'
columns name varchar2 position next
, segment varchar2 position next
, balance number position next
, credit_limit number position next
) cus
-------------------
20000
次の例では、顧客セグメントごとの平均与信限度額を取得します。たとえば、セグメント間の与信リスクに対する許容度を比較する場合に使用します:
select cus.segment
, avg(cus.credit_limit)
from csvtable
( passing 'Acme Industries#Manufacturing#12500#50000|Bergmann GmbH#Manufacturing#0#25000|Carlson Ltd#Wholesale#2500#10000|Dubois SA#Wholesale#7500#10000|Everest BV#Services##5000'
row delimiter '|'
column delimiter '#'
columns name varchar2 position next
, segment varchar2 position next
, balance number position next
, credit_limit number position next
) cus
group
by cus.segment
order
by cus.segment
-------------------
Manufacturing 37500
Services 5000
Wholesale 10000