Generating a View From Dynamic Query Bigquery - sql

We are generating a dynamic query using the script below :
execute immediate (select
''' select event_id, value, ''' || string_agg('''
(select value from b.key_value where key = "''' || key_name || '''") as ''' || key_name , ''', ''')
|| '''
from (
select event_id, value,
array(
select as struct
json_extract_scalar(kv, '$.key') key,
json_extract_scalar(kv, '$.value') value
from a.kvs kv
) key_value
from `project.dataset.table`,
unnest([struct(json_extract_array(dimensions, '$.key_value') as kvs)]) a
) b
'''
from (
select distinct json_extract_scalar(kv, '$.key') key_name
from `project.dataset.table`,
unnest(json_extract_array(dimensions, '$.key_value')) as kv
)
)
We want to create a view out of this is there any way in BQ by which we can create a view from this, so as user we can directly query our view.

Should be as simple as just adding create or replace view project.dataset.myview as to original query
execute immediate (select
''' create or replace view `project.dataset.myview` as select event_id, value, ''' || string_agg('''
(select value from b.key_value where key = "''' || key_name || '''") as ''' || key_name , ''', ''')
|| '''
from (
select event_id, value,
array(
select as struct
json_extract_scalar(kv, '$.key') key,
json_extract_scalar(kv, '$.value') value
from a.kvs kv
) key_value
from `project.dataset.table`,
unnest([struct(json_extract_array(dimensions, '$.key_value') as kvs)]) a
) b
'''
from (
select distinct json_extract_scalar(kv, '$.key') key_name
from `project.dataset.table`,
unnest(json_extract_array(dimensions, '$.key_value')) as kv
)
)

Related

Separate one column to some column in bigquery

I have table with one column called cols. I want to separate cols to new column based on type: SKU and MARK using bigquery.
cols
"dsc":[{"amount":30000,"c_amount":0,"d_amount":0,"d_id":"","scope":"CART","title":"Promo","type":"SKU"},{"amount":7000,"c_amount":0,"d_amount":7000,"d_id":"x","scope":"CART_D","title":"","type":"MARK"}]
The result i want is like:
sku_amount sku_c_amount sku_d_amount sku_d_id sku_scope sku_title mark_amount mark_c_amount mark_d_amount mark_d_id mark_scope
30000 0 0 CART Promo 7000 0 0 x CART_D
Anyone know the script? thank you
Consider below approach
create temp table temp_table as
select id1,
max(if(key = 'type', value, null)) over (partition by id1, id2) || '_' || key as key,
value, offset
from (
select md5(cols) as id1, md5(json) id2, arr[offset(0)] as key, arr[offset(1)] as value, offset
from your_table, unnest(json_extract_array('{' || cols || '}', '$.dsc')) json with offset,
unnest(split(translate(json, '{}"', ''))) kv,
unnest([struct(split(kv, ':') as arr)])
);
execute immediate (select '''
select * except(id1) from (select * except(offset) from temp_table)
pivot (any_value(value) for key in ("''' || string_agg(key, '","' order by offset, key) || '''"))
'''
from (select distinct key, offset from temp_table where not ends_with(key, '_type'))
);
if applied to sample data in your question - output is

GBQ Execute Immediate into a CTE

I am building an application/script for users that do not have write access to the database. Normally I would use Execute Immediate and save that result into a table, and then pull from that table and continue on with the script. Is there a way to save results from Execute Immediate in either a sub query or CTE so that the script can continue on?
Example code where results are put into a table
execute immediate (select '''create or replace table `project.dataset.table2` as
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as float64) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''')
;
I would need something more along the lines of this, but it doesn't work
WITH CTEtable2 as (
execute immediate (select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as float64) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''')
)
SELECT *
FROM CTEtable2

GBQ Execute Immediate Save Results to Table

Hi All I am trying to same the results from this query into a table on GBQ. I have linked the previous question for reference
GBQ Convert Data types en Mass
Starting Code
execute immediate (select '''
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as Numeric) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''');
Various Attempts
Create or Replace Table1 as
execute immediate (select '''
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as Numeric) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''');
execute immediate (select '''
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as Numeric) as ' || Fruit )
INTO Table1
from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''');
execute immediate (select '''
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as Numeric) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''') INTO Table1;
execute immediate (select '''
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as Numeric) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''' INTO Table1);
Use below approach
execute immediate (select '''create or replace table `project.dataset.table2` as
select `Group`, ''' || (select string_agg('cast(' || Fruit || ' as float64) as ' || Fruit ) from (
select regexp_extract_all(to_json_string((select as struct * except(`Group`) from unnest([t]))), r'"([^"]+)":') Fruits
from `project.dataset.table` t limit 1), unnest(Fruits) Fruit) ||
''' from `project.dataset.table`''');

Pivot BigQuery table using multiple rows

In order to pivot my big query table, I found this code
SELECT 'SELECT id, ' ||
STRING_AGG(
'MAX(IF(key = "' || key || '", value, NULL)) as `' || key || '`'
)
|| ' FROM `project.dataset.table` GROUP BY id ORDER BY id'
FROM (
SELECT key
FROM `project.dataset.table`
GROUP BY key
ORDER BY key
But even if I apply EXECUTE IMMEDIATE function, it returns a string of the code above.
What did I missed in that function ?
Thanks for your help
Use below
EXECUTE IMMEDIATE(
SELECT 'SELECT id, ' ||
STRING_AGG(
'MAX(IF(key = "' || key || '", value, NULL)) as `' || key || '`'
)
|| ' FROM `project.dataset.table` GROUP BY id ORDER BY id'
FROM (
SELECT key
FROM `project.dataset.table`
GROUP BY key
ORDER BY key
)
);

Creating BigQuery Stored Procedure by passing column and table name as parameters

I am trying to create BigQuery stored Procedure that will accept column and Table name as parameters.
This is the running Query:
EXECUTE IMMEDIATE (
''' SELECT
''' || (SELECT STRING_AGG(DISTINCT "MAX(IF(PROPERTY_KEY = '" || PROPERTY_KEY || "', a.RANK, NULL)) AS " || PROPERTY_KEY)
FROM `project.dataset.table` x
) || '''
FROM (
select "1" AS GroupbyCol,
PROPERTY_KEY ,
rank() over (order by PROPERTY_KEY ) AS RANK
from (
select distinct PROPERTY_KEY
from `project.dataset.table`
)
) a GROUP BY GroupbyCol
''')
I tried creating stored procedure as below:
CREATE OR REPLACE PROCEDURE `project.dataset.test_stored_procedure_ne`
(table_name STRING,
destination_table STRING,
row_ids STRING,
pivot_col_name STRING,
pivot_col_value STRING,
aggregation STRING)
BEGIN
DECLARE header STRING;
EXECUTE IMMEDIATE (
"SELECT
''' || (SELECT STRING_AGG(DISTINCT "MAX(IF(#pivot_col_name = '" || #pivot_col_name || "', a.RANK, NULL)) AS " || #pivot_col_name)
FROM `table_name` x
) || '''
FROM (
select "1" AS GroupbyCol,
#pivot_col_name ,
rank() over (order by #pivot_col_name ) AS RANK
from (
select distinct #pivot_col_name
from `table_name`
)
) a GROUP BY GroupbyCol
") header
USING pivot_col_name AS pivot_col_name
END;
Calling SP as :
CALL `project.dataset.test_stored_procedure_ne`(
'project.dataset.InputTbl' #table_name
, 'project.dataset.outputTbl' #destination_table
, 'Id' #row_id
, 'PROPERTY_KEY' # column name
, 'PROPERTY_VALUE' #column value
, 'MAX' #aggregation if any
);
Getting Error as :
Error validating procedure body (add OPTIONS(strict_mode=false) to suppress): Query error: Invalid value: Table name "table_name" missing dataset while no default dataset is set in the request.
Please explain how and when to put quotes which I believe am misplacing
Please explain how and when to put quotes which I believe am misplacing
You have below in few places
FROM `table_name`
Just replace those with below
FROM `"||table_name||"`