I know it isn’t a “good way of doing things”, but we use the following notations in many of our SQL queries :
SELECT dim_1,dim_2,...,dim_n,agg_function(metric_1),...agg_function(metric_k) FROM table GROUP BY 1,2,...,n
I was wondering if there is an even more succinct way of writing it, e.g. GROUP BY 1-n
(Group by col_1 to col_n)
Advertisement
Answer
Assume you have table with dozen or more columns
Consider below template (note below offset
is zero-based) for BigQuery – should be perfect for POC type of work you mentioned
select ( select string_agg(kv) from unnest(split(trim(to_json_string(t), '{}'))) kv with offset where offset between 0 and N-1 ) dims, agg_function(metric_1),...agg_function(metric_k) from `project.dataset.table` t group by 1
Below is dummy/simplistic example
with `project.dataset.table` as ( select 1 col1, '2' col2, 3 col3, 4 col4 union all select 11, '2', 3, 41 ) select ( select string_agg(kv) from unnest(split(trim(to_json_string(t), '{}'))) kv with offset where offset in (1,2) ) dims, sum(col1) metric1, count(col1) metric2 from `project.dataset.table` t group by 1
with output