Project: YugabyteDB (App Definition and Development / Database)
File: cr-xform-to-joined-table-sql.md

title: Create procedure xformtocovidcastfbsurvey_results() linkTitle: Create xformtocovidcastfbsurvey_results() headerTitle: Create the procedure xformtocovidcastfbsurvey_results() description: Creates a procedure to project the data from the three staging tables into the final destination, joining on the primary keys. menu: v2.18: identifier: cr-xform-to-joined-table-sql parent: ingest-scripts weight: 30 type: docs Save this script as "cr-xform-to-covidcast-fb-survey-results.sql" ```plpgsql drop procedure if exists xformtocovidcastfbsurvey_results() cascade; create procedure xformtocovidcastfbsurvey_results() language plpgsql as $body$ declare -- Check that the staging tables have the expected names for their roles. -- Each subquery assignemnt will fail if doesn't return exactly one row. maskwearersname text not null := (select stagingtable from covidcastnames where stagingtable = 'maskwearers'); symptomsname text not null := (select stagingtable from covidcastnames where stagingtable = 'symptoms'); cmntysymptomsname text not null := (select stagingtable from covidcastnames where stagingtable = 'cmntysymptoms'); stmt text not null := ' insert into covidcastfbsurvey_results( survey_date, state, maskwearingpct, maskwearingstderr, maskwearingsample_size, symptomspct, symptomsstderr, symptomssamplesize, cmntysymptomspct, cmntysymptomsstderr, cmntysymptomssample_size) select timevalue, geovalue, m.value, m.stderr, round(m.sample_size), s.value, s.stderr, round(s.sample_size), c.value, c.stderr, round(c.sample_size) from ?1 as m inner join ?2 as s using (timevalue, geovalue) inner join ?3 as c using (timevalue, geovalue)'; begin drop table if exists covidcastfbsurvey_results cascade; create table covidcastfbsurvey_results( survey_date date not null, state text not null, maskwearingpct numeric not null, maskwearingstderr numeric not null, maskwearingsample_size int not null, symptoms_pct numeric not null, symptoms_stderr numeric not null, symptomssamplesize int not null, cmntysymptomspct numeric not null, cmntysymptomsstderr numeric not null, cmntysymptomssample_size int not null, constraint covidcastfbsurveyresultspk primary key (state, survey_date), constraint covidcastfbsurveyresultschkmaskwearingpct check(maskwearing_pct between 0 and 100), constraint covidcastfbsurveyresultschksymptomspct check(symptoms_pct between 0 and 100), constraint covidcastfbsurveyresultschkcmntysymptomspct check(cmntysymptoms_pct between 0 and 100), constraint covidcastfbsurveyresultschkmaskwearingstderr check(maskwearing_stderr > 0), constraint covidcastfbsurveyresultschksymptomsstderr check(symptoms_stderr > 0), constraint covidcastfbsurveyresultschkcmntysymptomsstderr check(cmntysymptoms_stderr > 0), constraint covidcastfbsurveyresultschkmaskwearingsamplesize check(maskwearingsample_size > 0), constraint covidcastfbsurveyresultschksymptomssamplesize check(symptomssample_size > 0), constraint covidcastfbsurveyresultschkcmntysymptomssamplesize check(cmntysymptomssample_size > 0) ); execute replace(replace(replace(stmt, '?1', maskwearersname), '?2', symptoms_name), '?3', cmntysymptomsname); end; $body$; ```