[英]SELECT if table exists in WITH clause (BigQuery)
I am creating a table to keep continuous login days which are the results of the combination of login_[today]
and continuous_login_[previous_day]
.我正在创建一个表来保持连续登录天数,这是
login_[today]
和continuous_login_[previous_day]
组合的结果。 However, I want to handle the exception if the table for [previous_day]
doesn't exist.但是,如果
[previous_day]
的表不存在,我想处理异常。
With the help of the references [1] , [2] , and [3] , I was able to retrieve the value (continuous logins) only if the table for the previous day exists like A
.在参考文献[1] 、 [2]和[3]的帮助下,只有当前一天的表像
A
一样存在时,我才能检索值(连续登录)。
However, I wasn't able to use the result of IF EXISTS
statement inside WITH
clause like B
.但是,我无法像
B
那样在WITH
子句中使用IF EXISTS
语句的结果。 How can I use the results of the IF EXISTS
in WTIH
clause.如何在
WTIH
子句中使用IF EXISTS
的结果。 Or is there any better way of doing this work?或者有没有更好的方法来完成这项工作?
A.一种。
-- Retrieve only if the table exists
IF EXISTS (
SELECT *
FROM `my_project.my_dataset.__TABLES_SUMMARY__`
WHERE table_id = CONCAT('_my_table_', '20201010') -- daily log
)
THEN
(
SELECT
pid, MAX(continuous_login_days) AS continuous_login_days
FROM `my_project.my_dataset._my_table_*`
WHERE _TABLE_SUFFIX = '20201010'
GROUP BY pid
);
ELSE
SELECT NULL;
END IF
B. B.
-- GOAL
WITH
...
, continuous_logins_table AS (
[above statement]
)
SELECT
...
IFNULL(continuous_logins_table.continuous_login_days, 0) + 1 AS continuous_login_days,
FROM
...
LEFT JOIN continuous_logins_table
SELECT
pid, MAX(continuous_login_days) AS continuous_login_days
FROM `my_project.my_dataset._my_table_*`
WHERE _TABLE_SUFFIX = '20201010'
AND EXISTS(
SELECT 1
FROM `my_project.my_dataset.__TABLES_SUMMARY__`
WHERE table_id = CONCAT('_my_table_', '20201010')
)
GROUP BY pid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.