Google BigQuery SQL:如何将过程转换为返回表的函数?

Posted

技术标签:

【中文标题】Google BigQuery SQL:如何将过程转换为返回表的函数?【英文标题】:Google BigQuery SQL: how to convert procedure to function that returns table? 【发布时间】:2021-03-07 18:15:02 【问题描述】:

我写了一个运行良好的程序:

CREATE OR REPLACE PROCEDURE project.dataset.test_procedure 
    (v_month int64, v_year int64)
BEGIN
    WITH CTE AS
    (
        SELECT 
            Date,
            User_ID,
            Amount
        FROM 
            test_table
    )
    SELECT 
        User_ID,
        SUM(Amount) AS Total_Amount
    FROM 
        CTE 
    WHERE 
        extract(month from Date) = v_month 
        AND extract(year from Date) = v_year;
END;

我需要创建一个函数,它通过两个变量返回一个表(包括 CTE 语句):

CREATE OR REPLACE FUNCTION project.dataset.test_function (v_month int64 , v_year int64 ) 
Returns  table
(
WITH CTE AS
(
SELECT Date,
       User_ID,
       Amount
FROM test_table
)
SELECT User_ID,
       sum(Amount) AS Total_Amount
FROM CTE 
WHERE extract(month from Date)=v_month and extract(year from Date)=v_year;
END;
)

我看过一些例子,但我不知道如何正确编写返回表的函数...

如何在 BigQuery 中正确执行?

【问题讨论】:

【参考方案1】:

请参阅下面的玩具示例,了解您正在寻找的确切内容

create temp table test_table as
  select 1 User_ID, 10 Amount, date '2021-01-01' Date union all 
  select 1, 20, '2021-01-01' union all 
  select 1, 30, '2021-02-01' union all 
  select 2, 40, '2021-01-01' 
;

create temp function test_function (v_month int64 , v_year int64 ) as (
array(
  select as struct
    Date,
    User_ID,
    Amount
  from test_table
  where extract(month from Date) = v_month 
  and extract(year from Date)=v_year
));
select User_ID,
  sum(Amount) AS Total_Amount
from unnest(test_function(1, 2021)) 
group by User_ID

有输出

【讨论】:

您好,感谢您的回答。可以像在标准 MS SQL 中那样创建标准(不是 temp )函数并通过两个变量调用它吗? 我使用 temp USF 只是为了让您可以测试和使用它。您可以用永久替换临时 USF,它仍然可以工作!【参考方案2】:

标准函数比较复杂,但也可以工作

CREATE OR REPLACE 
FUNCTION `project.dataset.test_procedure` (v_month int64, v_year int64)
AS (
    --------------------------------------------------------------------
    -- declare v_month int64;
    -- declare v_year  int64;
    --------------------------------------------------------------------
    -- select * 
    -- from unnest(`project.dataset.test_procedure`(v_month, v_year))
    --------------------------------------------------------------------
    ARRAY (
            WITH CTE AS
            (
                SELECT Date,User_ID,Amount
                FROM `project.dataset.test_table`
            )
            SELECT AS STRUCT User_ID, SUM(Amount) AS Total_Amount
            FROM CTE
            WHERE extract(month from Date) = v_month
              AND extract(year  from Date) = v_year
            GROUP BY User_ID
          )
  );

【讨论】:

【参考方案3】:
CREATE OR REPLACE 
TABLE FUNCTION `project.dataset.test_procedure`
(v_month int64, v_year int64)
AS (
    WITH CTE AS
    (
        SELECT Date,User_ID,Amount
        FROM `project.dataset.test_table`
    )
    SELECT User_ID, SUM(Amount) AS Total_Amount
    FROM CTE 
    WHERE extract(month from Date) = v_month 
      AND extract(year from Date) = v_year
    GROUP BY User_ID
);

https://cloud.google.com/bigquery/docs/reference/standard-sql/table-functions

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于Google BigQuery SQL:如何将过程转换为返回表的函数?的主要内容,如果未能解决你的问题,请参考以下文章

如何在Google BigQuery中管理架构迁移

自动将数据上传到 Google Cloud Storage 和 BigQuery

我们如何将大型数据集从 Google BigQuery 导入 R?

Google 标准 SQL UDF - 写入 BigQuery

google bigquery如何查询以太坊ethereum数据 sql怎么写

google bigquery如何查询以太坊ethereum数据 sql怎么写