简体   繁体   English

在使用计划查询运行 BigQuery 作业之前处理多个条件

[英]Handle multiple conditions before running a BigQuery job using a scheduled query

I plan to have a scheduled query in BigQuery where it run the some query to check for some conditions first.我计划在 BigQuery 中安排一个查询,它会在其中运行一些查询以首先检查某些条件。 Then, if all condition passed, it will run the aggregation query然后,如果所有条件都通过,它将运行聚合查询

What I can think of right now is to do something like this:我现在能想到的是做这样的事情:

declare A default (select count(*) from mydataset.mytable);  --SELECT query to get a value
declare B ...
declare C ... 
. . . -- get all of the value into a variable

-- Then use if-else condition e.g.
if A > 100 and B = C+D+E and B = F and ...
then
   -- run an aggregation queries
else
   select "not pass the condition"; -- don't run anything
end if; 

This way is workable but I want to make it more visible when some condition not met, I can view later that what condition is not passed.这种方式是可行的,但我想让它在某些条件不满足时更明显,我可以稍后查看什么条件没有通过。 Any idea to improve this when running in scheduled query?在计划查询中运行时有什么想法可以改善这一点吗?

Combining the use of the raise and multiple conditional tests to specify the specific error you can use the following:结合使用 raise 和多个条件测试来指定特定错误,您可以使用以下内容:

BEGIN
    DECLARE A DEFAULT (SELECT COUNT(*) FROM `ds.table`);
    DECLARE B DEFAULT (SELECT COUNT(*) FROM `ds.table`);

    DECLARE C1 DEFAULT A>1;
    DECLARE C2 DEFAULT B>A;

    IF C1 AND C2 THEN
        SELECT "Execute Query";
    ELSE 
        IF NOT (C1) THEN raise USING message="Condition 1 failed"; END IF;
        IF NOT (C2) THEN raise USING message="Condition 2 failed"; END IF;
    END IF; 

EXCEPTION WHEN ERROR THEN 
    SELECT @@error.message;
END;

在此处输入图像描述 在此处输入图像描述

But if you will have multiple faillling conditions, its better to use a select:但是,如果您有多个失败条件,最好使用 select:


    DECLARE A DEFAULT (SELECT COUNT(*) FROM `ds.table`);
    DECLARE B DEFAULT (SELECT COUNT(*) FROM `ds.table`);

    DECLARE C1 DEFAULT A>10;
    DECLARE C2 DEFAULT B>A;

    IF C1 AND C2 THEN
        SELECT "Execute Query";
    ELSE 
        IF NOT (C1) THEN select "Condition 1 failed"; END IF;
        IF NOT (C2) THEN select "Condition 2 failed"; END IF;
    END IF; 

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM