I'd simplify the query. I don't see a need for the second CTE - or any CTE for that matter:
SELECT (date_trunc('week', e.soc_date))::date AS year_week, * -- ??
FROM p148.referral e
JOIN p148.contact_accounts ca ON e.contact_account_id = ca.id
JOIN public.owner1 o USING (name) -- don't duplicate the column "name"
WHERE e.soc_date >= '2018-01-01'
AND e.soc_date <= '2021-09-30'
The USING
clause only outputs a single copy of the join column name
.
I still strongly doubt you want SELECT *
. Will probably produce multiple id
columns, which would have to be renamed. And it's pointless to store the effectively identical e.contact_account_id
and c.id
...
Get the set of existing schemas in the database (minus system schemas you'll want to exclude):
SELECT nspname
FROM pg_catalog.pg_namespace
WHERE nspname NOT LIKE 'pg\_%'
AND nspname <> 'information_schema';
Related:
Now build one copy of the query for every schema and execute dynamically. One option: with \gexec
in psql:
SELECT format(
$$INSERT INTO public.my_table (year_week, ...)
SELECT (date_trunc('week', e.soc_date))::date AS year_week, *
FROM %1$I.referral e
JOIN %1$I.contact_accounts ca ON e.contact_account_id = ca.id
JOIN public.owner1 o USING (name) -- don't duplicate the column "name"
WHERE e.soc_date >= '2018-01-01'
AND e.soc_date <= '2021-09-30'$$
, nspname)
FROM pg_catalog.pg_namespace
WHERE nspname NOT LIKE 'pg\_%'
AND nspname <> 'information_schema'\gexec
See:
Use format()
with the format specifier %I
as demonstrated to properly quote concatenated names and defend against SQL injection. See:
There are other options, it all depends ...