【问题标题】:Iterate over rows in Stored Procedure BigQuery迭代存储过程 BigQuery 中的行
【发布时间】:2019-12-18 16:35:27
【问题描述】:

有没有办法在存储过程中获取查询结果,然后遍历 BigQuery 中的行?每行都有一个光标之类的东西。

这是我的存储过程,它需要 6 个参数,我从表中获取这些参数。我想调用该过程 X 次(X 是我的输入表的行数)。所以像:

对于 device_id、nb_measures、delta_t_min、delta_t_last_rec、date_cr、帧 IN(选择 device_id、nb_measures、delta_t_min、delta_t_last_rec、date_cr、frame FROM my_project.my_dataset.my_table

BEGIN
  DECLARE count INT64 DEFAULT 0;
  SET temp_list = [];
  WHILE count < nb_measures DO
    SET temperature = `bdz-dts-datascience-dev.fonctions.hexStringToInt`(frame, 5 + count, 1, 0, 8);
    IF temperature != 127 THEN
      IF count = 0 THEN
        SET measure_time = TIMESTAMP_SUB(date_cr, INTERVAL delta_t_last_rec MINUTE);
      ELSE
        SET measure_time = TIMESTAMP_SUB(date_cr, INTERVAL delta_t_last_rec + count * delta_t_min MINUTE);
      END IF;
      INSERT `20191218_temperature_repeteurs.step_2`(device_id, measure_time, temperature)
        VALUES(measure_time, temperature);
  END IF;
  END WHILE;
END;

或者另一方面,有没有办法在 SELECT 查询中执行存储过程来遍历结果?

SELECT 
  device_id, nb_mesures, delta_t_min, delta_t_last_rec, date_cr, frame
  CALL `my-dataset.my_procedure`(device_id, nb_mesures, delta_t_min, delta_t_last_rec, date_cr, frame)
FROM `my_project.my_dataset.my_table`)

【问题讨论】:

    标签: stored-procedures google-bigquery


    【解决方案1】:

    这是一个示例,说明如何使用取自 here 的存储过程对行进行迭代

    -- The input variable is employee’s employee_id (target_employee_id)
      -- The output variable (OUT) is employee_hierarchy which lists
      --      the employee_id of the employee’s manager
    CREATE PROCEDURE dataset.GetEmployeeHierarchy(
      target_employee_id INT64, OUT employee_hierarchy ARRAY<INT64>)
    BEGIN
      -- Iteratively search for this employee's manager, then the manager's
      -- manager, etc. until reaching the CEO, who has no manager.
      DECLARE current_employee_id INT64 DEFAULT target_employee_id;
      SET employee_hierarchy = [];
      WHILE current_employee_id IS NOT NULL DO
        -- Add the current ID to the array.
        SET employee_hierarchy =
          ARRAY_CONCAT(employee_hierarchy, [current_employee_id]);
        -- Get the next employee ID by querying the Employees table.
        SET current_employee_id = (
          SELECT manager_id FROM dataset.Employees
          WHERE employee_id = current_employee_id
        );
      END WHILE;
    END;
    

    【讨论】:

    • 我在 Google 的存储过程教程中看到了该示例,但是我不想做的是在没有任何条件的情况下循环查询结果。我会更新我的问题以使其更清楚
    【解决方案2】:

    不能在 SELECT 中调用存储过程。你可以做的是创建一个用户定义的函数[1],例如:

    CREATE TEMP FUNCTION my_udf(a INT64, b INT64, c INT64, d INT64) AS (....);
    

    然后:

    SELECT 
      device_id, nb_mesures, delta_t_min, delta_t_last_rec, date_cr, frame,
      my_udf(device_id, nb_mesures, delta_t_min, delta_t_last_rec, date_cr, frame)
    FROM `my_project.my_dataset.my_table`)
    

    [1]https://cloud.google.com/bigquery/docs/reference/standard-sql/user-defined-functions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-05
      • 1970-01-01
      • 2021-05-12
      • 2022-10-05
      • 2021-12-28
      相关资源
      最近更新 更多