最大値/最小値の場合、 レコードの参照(LookupRecord)でよい。引数InにはOrder By句を指定し昇順降順を指定する。参照されるレコードが複数となる可能性があるが、ひとつ目のレコードを参照するから降順であれば最大値、昇順であれば最小値を取得できる。ただし、昇順とした場合、フィールドの値がNullが先頭レコードになるから、Where句もしくはWhere条件(Where Condition)で、Is Not Nullとする必要がある。
平均値については、レコードごと(ForEachRecord)を使って、レコード数と合計を計算すればいい。
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<DataMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/11/application">
<DataMacro Name="FuncMaxMinAvr">
<Parameters>
<Parameter Name="paramFunc" Description="Max/Min/Avr rtnMaxMiniAvr" />
</Parameters>
<Statements>
<ConditionalBlock>
<If>
<Condition>[paramFunc]="Max"</Condition>
<Statements>
<LookUpRecord>
<Data Alias="T">
<Query>
<References>
<Reference Source="table01" />
</References>
<Results>
<Property All="true" />
</Results>
<Ordering>
<Order Direction="Descending" Source="table01" Name="Field01" />
</Ordering>
</Query>
</Data>
<Statements>
<Action Collapsed="true" Name="SetReturnVar">
<Argument Name="Name">rtnMaxMiniAvr</Argument>
<Argument Name="Value">[T].[Field01]</Argument>
</Action>
</Statements>
</LookUpRecord>
</Statements>
</If>
</ConditionalBlock>
<ConditionalBlock>
<If>
<Condition>[paramFunc]="Min"</Condition>
<Statements>
<LookUpRecord>
<Data Alias="T">
<Query>
<References>
<Reference Source="table01" />
</References>
<Results>
<Property All="true" />
</Results>
<Ordering>
<Order Source="table01" Name="Field01" />
</Ordering>
</Query>
<WhereCondition>[table01].[Field01] Is Not Null</WhereCondition>
</Data>
<Statements>
<Action Collapsed="true" Name="SetReturnVar">
<Argument Name="Name">rtnMaxMiniAvr</Argument>
<Argument Name="Value">[T].[Field01]</Argument>
</Action>
</Statements>
</LookUpRecord>
</Statements>
</If>
</ConditionalBlock>
<ConditionalBlock>
<If>
<Condition>[paramFunc]="Avr"</Condition>
<Statements>
<Action Collapsed="true" Name="SetLocalVar">
<Argument Name="Name">varCnt</Argument>
<Argument Name="Value">0</Argument>
</Action>
<Action Collapsed="true" Name="SetLocalVar">
<Argument Name="Name">varSum</Argument>
<Argument Name="Value">0</Argument>
</Action>
<ForEachRecord>
<Data>
<Reference>table01</Reference>
<WhereCondition>[table01].[Field01] Is Not Null</WhereCondition>
</Data>
<Statements>
<Action Collapsed="true" Name="SetLocalVar">
<Argument Name="Name">varCnt</Argument>
<Argument Name="Value">[varCnt]+1</Argument>
</Action>
<Action Collapsed="true" Name="SetLocalVar">
<Argument Name="Name">varSum</Argument>
<Argument Name="Value">[varSum]+[table01].[Field01]</Argument>
</Action>
</Statements>
</ForEachRecord>
<ConditionalBlock>
<If>
<Condition>[varCnt]>0</Condition>
<Statements>
<Action Collapsed="true" Name="SetReturnVar">
<Argument Name="Name">rtnMaxMiniAvr</Argument>
<Argument Name="Value">[varSum]/[varCnt]</Argument>
</Action>
</Statements>
</If>
</ConditionalBlock>
</Statements>
</If>
</ConditionalBlock>
</Statements>
</DataMacro>
</DataMacros>
重要な補足- レコードの参照(LookupRecord)/レコードごと(ForEachRecord) は、AccessServicesの設定のクエリ 1 つあたりの最大行数(Maximum Rows per query)に影響を受ける。したがって、設定の既定値を超えて実行しようとすると、エラーが発生しマクロは終了する。
- データマクロ実行中、レコードはロックされないから、必要な正確さを満たさないことがある。
0 件のコメント:
コメントを投稿