diff --git a/cumulus_library_covid/covid_symptom/count.py b/cumulus_library_covid/covid_symptom/count.py index 379c1ad..e49cdb8 100644 --- a/cumulus_library_covid/covid_symptom/count.py +++ b/cumulus_library_covid/covid_symptom/count.py @@ -120,8 +120,8 @@ def write_view_sql(view_list_sql: List[str], filename="count.sql") -> None: """ sql_optimizer = concat_view_sql(view_list_sql) sql_optimizer = sql_optimizer.replace("ORDER BY cnt desc", "") - sql_optimizer = sql_optimizer.replace("CREATE or replace VIEW", 'CREATE TABLE') - with open(filename, 'w') as fout: + sql_optimizer = sql_optimizer.replace("CREATE or replace VIEW", "CREATE TABLE") + with open(filename, "w") as fout: fout.write(sql_optimizer) @@ -134,7 +134,7 @@ def write_view_sql(view_list_sql: List[str], filename="count.sql") -> None: count_pcr("week"), count_study_period("month"), count_study_period("week"), - count_prevalence_ed('month'), - count_prevalence_ed('week'), + count_prevalence_ed("month"), + count_prevalence_ed("week"), ] ) diff --git a/cumulus_library_covid/covid_symptom/count.sql b/cumulus_library_covid/covid_symptom/count.sql index 35e0fd7..2e8ef49 100644 --- a/cumulus_library_covid/covid_symptom/count.sql +++ b/cumulus_library_covid/covid_symptom/count.sql @@ -1,151 +1,261 @@ -- ########################################################### -CREATE TABLE covid_symptom__count_dx_month AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , cond_month, enc_class_code, age_at_visit, ed_note, variant_era - FROM covid_symptom__dx - group by CUBE - ( cond_month, enc_class_code, age_at_visit, ed_note, variant_era ) - ) - select - cnt_encounter as cnt - , cond_month, enc_class_code, age_at_visit, ed_note, variant_era - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_dx_month AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + cond_month, + enc_class_code, + age_at_visit, + ed_note, + variant_era + FROM covid_symptom__dx + GROUP BY cube(cond_month, enc_class_code, age_at_visit, ed_note, variant_era) +) + +SELECT + cnt_encounter AS cnt, + cond_month, + enc_class_code, + age_at_visit, + ed_note, + variant_era +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_dx_week AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , cond_week, enc_class_code, age_at_visit, ed_note, variant_era - FROM covid_symptom__dx - group by CUBE - ( cond_week, enc_class_code, age_at_visit, ed_note, variant_era ) - ) - select - cnt_encounter as cnt - , cond_week, enc_class_code, age_at_visit, ed_note, variant_era - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_dx_week AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + cond_week, + enc_class_code, + age_at_visit, + ed_note, + variant_era + FROM covid_symptom__dx + GROUP BY cube(cond_week, enc_class_code, age_at_visit, ed_note, variant_era) +) + +SELECT + cnt_encounter AS cnt, + cond_week, + enc_class_code, + age_at_visit, + ed_note, + variant_era +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_pcr_month AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , covid_pcr_month, covid_pcr_result_display, variant_era, ed_note, age_at_visit, gender, race_display - FROM covid_symptom__pcr - group by CUBE - ( covid_pcr_month, covid_pcr_result_display, variant_era, ed_note, age_at_visit, gender, race_display ) - ) - select - cnt_encounter as cnt - , covid_pcr_month, covid_pcr_result_display, variant_era, ed_note, age_at_visit, gender, race_display - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_pcr_month AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + covid_pcr_month, + covid_pcr_result_display, + variant_era, + ed_note, + age_at_visit, + gender, + race_display + FROM covid_symptom__pcr + GROUP BY + cube( + covid_pcr_month, + covid_pcr_result_display, + variant_era, + ed_note, + age_at_visit, + gender, + race_display + ) +) + +SELECT + cnt_encounter AS cnt, + covid_pcr_month, + covid_pcr_result_display, + variant_era, + ed_note, + age_at_visit, + gender, + race_display +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_pcr_week AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , covid_pcr_week, covid_pcr_result_display, variant_era, ed_note, age_at_visit, gender, race_display - FROM covid_symptom__pcr - group by CUBE - ( covid_pcr_week, covid_pcr_result_display, variant_era, ed_note, age_at_visit, gender, race_display ) - ) - select - cnt_encounter as cnt - , covid_pcr_week, covid_pcr_result_display, variant_era, ed_note, age_at_visit, gender, race_display - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_pcr_week AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + covid_pcr_week, + covid_pcr_result_display, + variant_era, + ed_note, + age_at_visit, + gender, + race_display + FROM covid_symptom__pcr + GROUP BY + cube( + covid_pcr_week, + covid_pcr_result_display, + variant_era, + ed_note, + age_at_visit, + gender, + race_display + ) +) + +SELECT + cnt_encounter AS cnt, + covid_pcr_week, + covid_pcr_result_display, + variant_era, + ed_note, + age_at_visit, + gender, + race_display +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_study_period_month AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , start_month, variant_era, ed_note, gender, age_group, race_display - FROM covid_symptom__study_period - group by CUBE - ( start_month, variant_era, ed_note, gender, age_group, race_display ) - ) - select - cnt_encounter as cnt - , start_month, variant_era, ed_note, gender, age_group, race_display - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_study_period_month AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + start_month, + variant_era, + ed_note, + gender, + age_group, + race_display + FROM covid_symptom__study_period + GROUP BY cube(start_month, variant_era, ed_note, gender, age_group, race_display) +) + +SELECT + cnt_encounter AS cnt, + start_month, + variant_era, + ed_note, + gender, + age_group, + race_display +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_study_period_week AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , start_week, variant_era, ed_note, gender, age_group, race_display - FROM covid_symptom__study_period - group by CUBE - ( start_week, variant_era, ed_note, gender, age_group, race_display ) - ) - select - cnt_encounter as cnt - , start_week, variant_era, ed_note, gender, age_group, race_display - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_study_period_week AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + start_week, + variant_era, + ed_note, + gender, + age_group, + race_display + FROM covid_symptom__study_period + GROUP BY cube(start_week, variant_era, ed_note, gender, age_group, race_display) +) + +SELECT + cnt_encounter AS cnt, + start_week, + variant_era, + ed_note, + gender, + age_group, + race_display +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_prevalence_ed_month AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , author_month, covid_dx, covid_icd10, covid_pcr_result, covid_symptom, symptom_icd10_display, variant_era, age_group - FROM covid_symptom__prevalence_ed - group by CUBE - ( author_month, covid_dx, covid_icd10, covid_pcr_result, covid_symptom, symptom_icd10_display, variant_era, age_group ) - ) - select - cnt_encounter as cnt - , author_month, covid_dx, covid_icd10, covid_pcr_result, covid_symptom, symptom_icd10_display, variant_era, age_group - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_prevalence_ed_month AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + author_month, + covid_dx, + covid_icd10, + covid_pcr_result, + covid_symptom, + symptom_icd10_display, + variant_era, + age_group + FROM covid_symptom__prevalence_ed + GROUP BY + cube( + author_month, + covid_dx, + covid_icd10, + covid_pcr_result, + covid_symptom, + symptom_icd10_display, + variant_era, + age_group + ) +) + +SELECT + cnt_encounter AS cnt, + author_month, + covid_dx, + covid_icd10, + covid_pcr_result, + covid_symptom, + symptom_icd10_display, + variant_era, + age_group +FROM powerset +WHERE cnt_subject >= 10; -- ########################################################### -CREATE TABLE covid_symptom__count_prevalence_ed_week AS - with powerset as - ( - select - count(distinct subject_ref) as cnt_subject - , count(distinct encounter_ref) as cnt_encounter - , author_week, covid_dx, covid_icd10, covid_pcr_result, covid_symptom, symptom_icd10_display, variant_era, age_group - FROM covid_symptom__prevalence_ed - group by CUBE - ( author_week, covid_dx, covid_icd10, covid_pcr_result, covid_symptom, symptom_icd10_display, variant_era, age_group ) - ) - select - cnt_encounter as cnt - , author_week, covid_dx, covid_icd10, covid_pcr_result, covid_symptom, symptom_icd10_display, variant_era, age_group - from powerset - WHERE cnt_subject >= 10 - ; +CREATE TABLE covid_symptom__count_prevalence_ed_week AS +WITH powerset AS ( + SELECT + count(DISTINCT subject_ref) AS cnt_subject, + count(DISTINCT encounter_ref) AS cnt_encounter, + author_week, + covid_dx, + covid_icd10, + covid_pcr_result, + covid_symptom, + symptom_icd10_display, + variant_era, + age_group + FROM covid_symptom__prevalence_ed + GROUP BY + cube( + author_week, + covid_dx, + covid_icd10, + covid_pcr_result, + covid_symptom, + symptom_icd10_display, + variant_era, + age_group + ) +) + +SELECT + cnt_encounter AS cnt, + author_week, + covid_dx, + covid_icd10, + covid_pcr_result, + covid_symptom, + symptom_icd10_display, + variant_era, + age_group +FROM powerset +WHERE cnt_subject >= 10;