Bigquery + Firebase analytics: select table of yesterday - google-bigquery

I'm querying bigQuery like this:
SELECT
param1.value.string_value AS id,
count(1) AS views,
FROM `projectname.analytics_id.events_20200808`,
UNNEST(event_params) as param1
WHERE
event_name = 'myEvent'
AND param1.key='id'
GROUP BY 1
I would like to make this a scheduled query. Is there a possibility to make my table a variable (so I can query the table of yesterday automatically)?
projectname.analytics_id.events_20200808 -> projectname.analytics_id.events_VARIABLE
Thanks!

You can use dynamic SQL option by using EXECUTE IMMEDIATE. Here is the example:
DECLARE TABLE_NAME STRING;
SET TABLE_NAME=CONCAT("projectname.analytics_id.events_",FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)));
EXECUTE IMMEDIATE FORMAT("""SELECT * FROM %s""",TABLE_NAME);

Related

How to create a reference value in Oracle SQl Developer

I use basic sql querying for my day to day work, but I regularly find myself needing to run queries in different tables using the same where clauses.
What I would ideally like to do is locally set a value to a name, for example:
traderef = ABCD1234. It's kind of like Defining a name in excel.
and then use 'traderef' in my queries,
select * from table1 where tranid = traderef
select * from table2 where tranid = traderef and otherattr = 'xyz1'
I only have query access to the dbs that i use, i have tried to google results, and found some info re SET
TIA
Declare a bind variable using the SQL/Plus and SQL Developer command VARIABLE name data_type and assign it a value using EXECUTE (or a PL/SQL anonymous block) and then use it in your queries:
VARIABLE traderef VARCHAR2(20)
EXEC :traderef := 'ABCD1234';
SELECT * FROM table1 WHERE tranid = :traderef;
SELECT * FROM table2 HWERE tranid = :traderef AND otherattr = 'xyz1';

Using String to Select From

So I've got a tables being imported daily from firebase and I'm exporting them from google cloud to power bi. The naming schema stays roughly the same, the only thing that changes daily is the date at the end of the table name. Been trying to create a query that makes the process fully automated, but I've been stuck. The concat is correct, but the string doesn't work to call from. Help is appreciated.
DECLARE Query STRING(100);
SET Query = CONCAT("report-viewer.analytics_278375497.events_", FORMAT_DATE("%Y%m%d", CURRENT_DATE()));
SELECT
//Various stuff
FROM Query LIMIT 1000
You should use EXECUTE IMMEDIATE in this scenario
Using your particular simplified example - it could look like below
DECLARE table_reference STRING(100);
SET table_reference = 'report-viewer.analytics_278375497.events_' || FORMAT_DATE('%Y%m%d', CURRENT_DATE());
EXECUTE IMMEDIATE (
'''SELECT //Various stuff
FROM `''' || table_reference || '''` LIMIT 1000
''');

SQL DB2 - Execute a Query using a results from another table or query

I am running the following query:
select TABLE_SCHEMA ||'.'||TABLE_NAME AS Schema_Table
from qsys2.systables
where table_schema = 'PTLDBA'
AND TABLE_NAME LIKE'DAILY%'
AND DATE (LAST_ALTERED_TIMESTAMP) = CURRENT DATE;
The result set is schema.table name. I need to use the result set to query the actual table
The final query would look like this:
Select * from Schema_Table limit 10;
The table name changes depending on the day. I believe a SQL Procedure would work best but I am not sure how to write it.

Is there any way to execute a dynamic query in bigquery?

Folks, I need to do a dynamic select in a table, using the variable after from, like this:
SELECT * FROM #table.
If anyone can do this otherway, please help!
tks!
I'm using this query below:
DECLARE id INT64;
DECLARE contador INT64 DEFAULT 1;
DECLARE name_table STRING;
DECLARE query STRING;
CREATE OR REPLACE TABLE `crm-prod-254714.work.controle_tabelas`
AS
SELECT DISTINCT CONCAT('`', table_catalog, '.', table_schema, '.', table_name, '`') as tabela
,ROW_NUMBER() OVER() AS ROWID
FROM ntk.INFORMATION_SCHEMA.TABLES;
SET id = (SELECT MAX(ROWID) FROM `crm-prod-254714.work.controle_tabelas`);
WHILE (contador <= id) DO
SET name_table = (SELECT tabela FROM `crm-prod-254714.work.controle_tabelas` WHERE rowid = 1);
SET QUERY = CONCAT('SELECT * FROM ', name_table, ';');
EXECUTE QUERY;
SET contador = contador+1;
END WHILE
As of 5/20/2020, BigQuery released dynamic SQL feature for you to achieve the goal.
Dynamic SQL is now available as a beta release in all BigQuery regions. Dynamic SQL lets you generate and execute SQL statements dynamically at runtime. For more information, see EXECUTE IMMEDIATE.
For your scenario, you only need to make change to one line:
EXECUTE QUERY;
=>
EXECUTE IMMEDIATE QUERY;

How to extract part of SQL query from a table

Is it possible to to extract SQL queries that are saved in a table?
For example
select * from saved_queries
name | statement
queryname | 'select * from mytable where myfield = 'somevalue'
I would like to be able to do something like
select * from ( extractsomehow( 'select Statement from saved_queries where name = 'queryname') ).
Unfortunately I cannot use Java so I am restricted to SQL and XML there.
I am using Oracle 11g
If you can write a stored procedure, you could use execute immediate, something like this:
select statement into v_statement from saved_queries where ... ;
execute immediate v_statement;
Before you use dynamic SQL, think carefully about whether or not you actually need it.