さて、以下は3つのうち2つを行います:
select n,
(case when n = 1 then cast(cust_id as varchar(255)) else rpt_name end) as grouping,
avg(req_secs)
from t cross join
(select 1 as n union all select 2
) n
group by n, (case when n = 1 then cust_id else rpt_name end);
これは基本的にデータを「2 倍」にしてから、各グループの集計を行います。cust_id
これは、とrpt_name
が互換性のある型であることを前提としています。(そうでない場合は、クエリを微調整できます。)
実際には、以下を使用して全体の平均を取得できますrollup
。
select n,
(case when n = 1 then cust_id else rpt_name end) as grouping,
avg(req_secs)
from t cross join
(select 1 as n union all select 2
) n
group by n, (case when n = 1 then cast(cust_id as varchar(255)) else rpt_name end) with rollup
平均は元のデータと「2 倍」のデータで同じであるため、これは平均に対して機能します。sum()
またはには機能しませんcount()
。