grouping()
上位レベルの集計のためにディメンション値が null の場合 (通常、該当の行が小計または総計であることを意味します) は 1 を返し、それ以外の場合は 0 を返します。
grouping() 関数が最も役に立つのは、group ステートメントで rollup 修飾子を組み合わせて使用した場合です。grouping() を呼び出すことで、小計データを操作できます。
例 - 小計データへのラベル付け
商談情報のデータセットがあり、リードソースと種別ごとに金額が合計されているとします。合計を rollup によって計算します。次に、case ステートメントで grouping() を使用して、行が合計かどうかと、行に「All (すべて)」の値のラベルが付けられているかどうかを確認します。
1q = load "opportunityData";
2
3--Modify the group statement with rollup to calculate subtotals of grouped measures
4q = group q by rollup('Type', 'LeadSource');
5
6q = order q by ('Type', 'LeadSource');
7
8--Determine which rows are totals with grouping(), which returns 1 if a row is a total
9q = foreach q generate
10 (case
11 when grouping('Type') == 1 then "All Types"
12 else 'Type'
13 end) as 'Type',
14 (case
15 when grouping('LeadSource') == 1 then "All Lead Sources"
16 else 'LeadSource'
17 end) as 'LeadSource',
18 sum('Amount') as 'sum_Amount';