1

I am trying to extract some information from all tables in the SALES dataset. I want to run it for multiple clients. Each client has a big query project. Bellow query returns what I want for our London client. How I can use a wildcard to do the same query for the other 10 clients and union all in one table?

SELECT *
FROM   london_prod_uk_eu.sales.__tables__ 

Basically, I want to simplify the below query:

SELECT *
FROM   london_prod_uk_eu.sales.__tables__
UNION ALL
SELECT *
FROM   toronto_prod_can_us.sales.__tables__

SELECT *
FROM   rome_prod_it_eu.sales.__tables__
UNION ALL
SELECT *
FROM   madrid_prod_sp_eu.sales.__tables__ 

1 Answer 1

2

Consider below approach

declare query array<string> default [];
declare projects array<string>;
set projects = [
  'london-prod-uk-eu',
  'toronto-prod-ca-us',
  'rome-prod-it-eu',
  'madrid-prod-sp-eu'
];

for record IN (select project from unnest(projects) project) do
   set query =  query || [format('select * from `%s.SALES.__TABLES__`', record.project)];
end for;

execute immediate (select string_agg(line, ' union all ') from unnest(query) line); 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.