简体   繁体   English

根据 Bigquery 计划查询中的日期动态更改 FROM 表名称

[英]Change FROM Table Name Dynamically based on date in Bigquery Scheduled Query

在此处输入图像描述

Please refer the screenshot attached.. i need to set up a bigquery scheduled task to pull info into a permanent table.请参考随附的屏幕截图。我需要设置一个 bigquery 计划任务以将信息提取到永久表中。 the from table name will dynamically change each day so i can pull that day info only and append it to the existing table.来自表的名称每天都会动态更改,因此我只能将当天的信息和 append 提取到现有表中。 any help will be highly appreciated任何帮助将不胜感激在此处输入图像描述

please see the second image...i tried to achieve using this.. but i am unable to convert yester into String.. and add it..请看第二张图片……我试图用这个来实现……但我无法将 yester 转换成字符串……并添加它……

actually this was the query i am looking to implement this..实际上这是我要实现的查询..

SELECT event_date, event_timestamp, event_name, (select value.double_value from unnest(event_params) where key = 'percentage') as percentage, (select value.double_value from unnest(event_params) where key = 'seconds') as seconds FROM xscore-prod.analytics_229726387.events_* WHERE event_name = "spent_time_in_activity" SELECT event_date, event_timestamp, event_name, (select value.double_value from unnest(event_params) where key = 'percentage') 作为百分比, (select value.double_value from unnest(event_params) where key = 'seconds') as seconds FROM xscore-prod .analytics_229726387.events_* WHERE event_name = "spent_time_in_activity"

how can i implement the script into this我怎样才能将脚本实现到这个

You can use scripting to generate and execute queries dynamically in BigQuery:您可以使用脚本在 BigQuery 中动态生成和执行查询:

DECLARE yesterday STRING DEFAULT FORMAT_DATE("%F", (DATE_ADD(CURRENT_DATE(), INTERVAL -1 DAY)));
DECLARE query STRING;
SET query = "SELECT * FROM `xscore-prod.analytics_229726387.events_" || yesterday || "` LIMIT 1000";
EXECUTE IMMEDIATE query;

You can do this:你可以这样做:

select * from <project>.<dataset>.events_* where _TABLE_SUFFIX = '20220525' select * 来自<project>.<dataset>.events_*其中 _TABLE_SUFFIX = '20220525'

Seehttps://cloud.google.com/bigquery/docs/querying-wildcard-tables请参阅https://cloud.google.com/bigquery/docs/querying-wildcard-tables

If your table comes from Firebase Analytics, try this:如果您的表格来自 Firebase Analytics,请尝试以下操作:

WHERE
  event_name = 'your_event_name'
AND _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 2 DAY))

Here I'm querying yesterday only as your example.在这里我只查询昨天作为你的例子。

Make sure you have an underscore after the table name.确保表名后有下划线。 The format of the google analytics table is analytics_(propertyid).events_(datestring) google analytics表的格式是analytics_(propertyid).events_(datestring)

select event_date,sum(case when event_name="session_start" then 1 else 0 end  ) as session,sum(case when event_name="add_to_cart" then 1 else 0 end  ) as atc,traffic_source.name,traffic_source.medium,traffic_source.source from `your_project_name.analytics_(propertyid).events_*`
WHERE _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 180 DAY))
group by 1,4,5,6

Moreover,而且,

    _TABLE_SUFFIX >= FORMAT_TIMESTAMP('%Y%m%d', TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 day)) ),

is totally legit完全合法

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

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