BigQuery根据结果运行不同的查询?

时间:2017-04-12 14:48:37

标签: sql google-bigquery

我不知道如何做到这一点,并查看了if函数,但似乎无法让它工作。在伪代码中

data(if (q1, truequery, falsequery)) select ... from `data`

基本上我想要做的是如果查询返回结果,运行一个查询,如果不运行另一个查询。任何帮助/领导都会很棒,谢谢!

1 个答案:

答案 0 :(得分:3)

尝试下面的BigQuery Standard SQL示例

   
#standardSQL
WITH tableA AS (
  SELECT 1 AS x
),
tableB AS (
  SELECT 2 AS x
),
truequery AS (
  SELECT * FROM tableA WHERE x = 1
),
falsequery AS (
  SELECT * FROM tableB
)
SELECT * FROM truequery UNION ALL 
SELECT * FROM falsequery WHERE (SELECT COUNT(1) FROM truequery) = 0  

希望这能给你带来好主意 请注意:两个查询的输出模式应该相同

  

我可以在另一个WITH allTables AS(...)中执行所有这些,然后对其运行查询吗?

#standardSQL
WITH tableA AS (
  SELECT 1 AS x
),
tableB AS (
  SELECT 2 AS x
),
truequery AS (
  SELECT * FROM tableA WHERE x = 777
),
falsequery AS (
  SELECT * FROM tableB
),
allTables AS (
  SELECT * FROM truequery UNION ALL 
  SELECT * FROM falsequery WHERE (SELECT COUNT(1) FROM truequery) = 0
)
SELECT * FROM allTables