有没有办法创建一个过程来解析具有嵌套字段的列,以便对所有列(甚至是嵌套列)执行计算?
我的问题是这个过程停留在第一级,只在那里执行我的计算,而不动态解析我的嵌套列。
CREATE OR REPLACE PROCEDURE `myprocedurename`(
_tableName STRING,
_destination STRING
)
BEGIN
DECLARE _columns STRING;
DECLARE _sql STRING;
SET _columns = (
SELECT STRING_AGG(
CASE
WHEN field_path = column_name THEN CONCAT("ROUND(COUNTIF(", column_name, " IS NOT NULL) * 100.0 / COUNT(*), 2) AS completude_", column_name) -- Handle top-level columns
ELSE CONCAT("ROUND(COUNTIF(", REPLACE(field_path, '.', '_'), " IS NOT NULL) * 100.0 / COUNT(*), 2) AS completude_", REPLACE(field_path, '.', '_')) -- Handle nested fields (but it takes into consideration only the top level of the nested columns)
END, ', ')
FROM `myproject.INFORMATION_SCHEMA.COLUMN_FIELD_PATHS`
WHERE table_name = _tableName);
SET _sql = CONCAT("CREATE OR REPLACE TABLE `mydataset.", _destination, "` AS SELECT ", _columns, " FROM `mydataset.", _tableName, "`");
EXECUTE IMMEDIATE _sql;
END;