I need to create a table with the same aggregations at multiple different levels in BigQuery, for example:
SELECT
dimension_a,
dimension_b,
SUM(value) AS value
FROM mydataset.table
GROUP BY dimension_a, dimension_b
UNION ALL
SELECT
dimension_a,
NULL AS dimension_b,
SUM(value) AS value
FROM mydataset.table
GROUP BY dimension_a
UNION ALL
SELECT
NULL AS dimension_a,
dimension_b,
SUM(value) AS value
FROM mydataset.table
GROUP BY dimension_b
I guess it is not the most elegant codes... for example, I have 18 different aggregation dimensions, that means I need to stack those similar code blocks 18 times with UNION ALL, I am wondering if there could be a function that allows me to have the aggregation dimension as the input?
For example, something like:
CREATE OR REPLACE TABLE FUNCTION mydataset.aggregation_dimension(X type, Y type)
AS
SELECT
X as dimension_a, Y as dimension_b,
SUM(value) AS value
FROM mydataset.table
GROUP BY X, Y;
SELECT * FROM mydataset.aggregation_dimension(dimension_a, dimension_b)
UNION ALL
SELECT * FROM mydataset.aggregation_dimension(dimension_a, NULL)
UNION ALL
SELECT * FROM mydataset.aggregation_dimension(NULL, dimension_b)
Where X, Y should be a column in the table mydataset.table ... However, I have no idea how to define the type of this kind of inputs. I also don't know if it is possible to have such a setting...
Thank you in advance for your help!