I have 4 tables (I actually have lots more that I want to do this with...but this is where I am starting).
distr_catalogs
: has manydistr_catalog_brands
,distr_catalog_system_types
distr_catalog_brands
: belongs todistr_catalogs
distr_catalog_system_types
: belongs todistr_catalogs
brand_catalog_sections
: belongs todistr_catalog_brands
,distr_catalog_system_types
I am wanting to create a materialized view whose columns are:
catalog_id | catalog_name | brand_ids | system_type_ids | catalog_sections
catalog_id
&catalog_name
come from thedistr_catalog
tablebrand_ids
holds an array of the brand ids related to the catalogsystem_type_ids
holds an array the system type ids related to the catalogcatalog_sections
holds an array of brand catalog section ids that are related to thebrand_ids
andsystem_type_ids
I can do all but the last just fine:
CREATE MATERIALIZED VIEW catalog_system_brands AS
select dc.id as catalog_id, dc.catalog_name,
ARRAY(SELECT brand_id FROM distr_catalog_brands WHERE distr_catalog_id = dc.id) as brands,
ARRAY(SELECT id FROM distr_catalog_system_types WHERE display_status = true AND distr_catalog_id = dc.id) as system_type_ids,
from distr_catalogs dc
This gets me a great table that has all the right data. However the only way I can figure out to get the last column I need ... is to completely re-write the subqueries in the final array statement. I get an error every time I try to use the brands
or system_type_ids
aliases I defined in the two above arrays.
CREATE MATERIALIZED VIEW catalog_system_brands AS
select dc.id as catalog_id, dc.catalog_name,
ARRAY(SELECT brand_id FROM distr_catalog_brands WHERE distr_catalog_id = dc.id) as brands,
ARRAY(SELECT id FROM distr_catalog_system_types WHERE display_status = true AND distr_catalog_id = dc.id) as system_type_ids,
ARRAY(SELECT id FROM brand_catalog_sections
WHERE brand_id = ANY(SELECT brand_id FROM distr_catalog_brands WHERE distr_catalog_id = dc.id)
AND system_type_id = ANY(SELECT system_type_id FROM distr_catalog_system_types WHERE display_status = true AND distr_catalog_id = dc.id)) as section_ids
from distr_catalogs dc
How would I re-write the create statement to not have to re-run the subqueries?
Note: This is the first instance of this. However, the next table I bring into this view will need to use the results of the section_ids
column to determine which systems to display. So this will get out of hand quickly if I am not able to reuse the results from a previous query.