diff --git a/dbt/sagerx/macros/active_and_prescribable.sql b/dbt/sagerx/macros/active_and_prescribable.sql new file mode 100644 index 00000000..f46c9891 --- /dev/null +++ b/dbt/sagerx/macros/active_and_prescribable.sql @@ -0,0 +1,12 @@ +{% macro active_and_prescribable() %} +case + when suppress = 'N' + then true + else false +end as active, +case + when cvf = '4096' + then true + else false +end as prescribable +{% endmacro %} diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_ingredients.sql b/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_ingredients.sql index 3ec95b47..ae11f6b7 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_ingredients.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_ingredients.sql @@ -1,17 +1,15 @@ -- int_mthspl_products_to_active_ingredients.sql -with - -substance as ( - - select * from {{ ref('stg_rxnorm__mthspl_substances') }} - -), - -product as ( +with substance as ( + SELECT * FROM {{ ref('stg_rxnorm__mthspl_substances') }} +) - select * from {{ ref('stg_rxnorm__mthspl_products') }} +, product as ( + SELECT * FROM {{ ref('stg_rxnorm__mthspl_products') }} +) +, rxnrel AS ( + SELECT * FROM {{ source('rxnorm', 'rxnorm_rxnrel') }} ) select distinct @@ -26,7 +24,7 @@ select distinct , substance.tty as active_ingredient_tty , product.active as active , product.prescribable as prescribable -from sagerx_lake.rxnorm_rxnrel rxnrel +from rxnrel inner join substance on rxnrel.rxaui1 = substance.rxaui inner join product diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_moieties.sql b/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_moieties.sql index f41f8606..d5d0f2c6 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_moieties.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_active_moieties.sql @@ -1,17 +1,15 @@ -- int_mthspl_products_to_active_moieties.sql -with - -substance as ( - - select * from {{ ref('stg_rxnorm__mthspl_substances') }} - -), - -product as ( +with substance as ( + SELECT * FROM {{ ref('stg_rxnorm__mthspl_substances') }} +) - select * from {{ ref('stg_rxnorm__mthspl_products') }} +, product as ( + SELECT * FROM {{ ref('stg_rxnorm__mthspl_products') }} +) +, rxnrel AS ( + SELECT * FROM {{ source('rxnorm', 'rxnorm_rxnrel') }} ) select distinct @@ -26,7 +24,7 @@ select distinct , substance.tty as active_moiety_tty , product.active as active , product.prescribable as prescribable -from sagerx_lake.rxnorm_rxnrel rxnrel +from rxnrel inner join substance on rxnrel.rxaui1 = substance.rxaui inner join product diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_inactive_ingredients.sql b/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_inactive_ingredients.sql index 6aa8b841..811cdf23 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_inactive_ingredients.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_mthspl_products_to_inactive_ingredients.sql @@ -1,17 +1,15 @@ -- int_mthspl_products_to_inactive_ingredients.sql -with - -substance as ( - - select * from {{ ref('stg_rxnorm__mthspl_substances') }} - -), - -product as ( +with substance as ( + SELECT * FROM {{ ref('stg_rxnorm__mthspl_substances') }} +) - select * from {{ ref('stg_rxnorm__mthspl_products') }} +, product as ( + SELECT * FROM {{ ref('stg_rxnorm__mthspl_products') }} +) +, rxnrel AS ( + SELECT * FROM {{ source('rxnorm', 'rxnorm_rxnrel') }} ) select distinct @@ -26,7 +24,7 @@ select distinct , substance.tty as inactive_ingredient_tty , product.active as active , product.prescribable as prescribable -from sagerx_lake.rxnorm_rxnrel rxnrel +from rxnrel inner join substance on rxnrel.rxaui1 = substance.rxaui inner join product diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_all_ndcs_to_product_rxcuis.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_all_ndcs_to_product_rxcuis.sql index d30edd1d..d3a93d1d 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_all_ndcs_to_product_rxcuis.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_all_ndcs_to_product_rxcuis.sql @@ -1,11 +1,11 @@ with all_ndcs as ( select * from {{ ref('stg_rxnorm__all_ndcs') }} -), +) -product_rxcuis as +, product_rxcuis as ( - select * from sagerx_lake.rxnorm_rxnconso + select * from {{ source('rxnorm', 'rxnorm_rxnconso') }} where sab = 'RXNORM' and tty in ('SCD', 'SBD', 'GPCK', 'BPCK') ) diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_clinical_product_components.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_clinical_product_components.sql index 07426229..68ffae26 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_clinical_product_components.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_clinical_product_components.sql @@ -1,29 +1,19 @@ -- int_rxnorm_clinical_products_to_clinical_product_components.sql -with - -ndc as ( - +with ndc as ( select * from {{ ref('stg_rxnorm__ndcs') }} +) -), - -rcp as ( - +, rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rcpcl as ( - +, rcpcl as ( select * from {{ ref('stg_rxnorm__clinical_product_component_links') }} +) -), - -rcpc as ( - +, rcpc as ( select * from {{ ref('stg_rxnorm__clinical_product_components') }} - ) select diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_dose_forms.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_dose_forms.sql index f08ac268..4702354b 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_dose_forms.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_dose_forms.sql @@ -1,29 +1,18 @@ -- int_rxnorm_clinical_products_to_dose_forms.sql - -with - -rcp as ( - +with rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rcpcl as ( - +, rcpcl as ( select * from {{ ref('stg_rxnorm__clinical_product_component_links') }} +) -), - -rcpc as ( - +, rcpc as ( select * from {{ ref('stg_rxnorm__clinical_product_components') }} +) -), - -rdf as ( - +, rdf as ( select * from {{ ref('stg_rxnorm__dose_forms') }} - ) select diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_components.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_components.sql index 5aff7d49..4007a592 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_components.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_components.sql @@ -1,47 +1,31 @@ -- int_rxnorm_clinical_products_to_ingredient_components.sql -with - -rcp as ( - +with rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rcpcl as ( - +, rcpcl as ( select * from {{ ref('stg_rxnorm__clinical_product_component_links') }} +) -), - -rcpc as ( - +, rcpc as ( select * from {{ ref('stg_rxnorm__clinical_product_components') }} +) -), - -rdf as ( - +, rdf as ( select * from {{ ref('stg_rxnorm__dose_forms') }} +) -), - -ri as ( - +, ri as ( select * from {{ ref('stg_rxnorm__ingredients') }} +) -), - -ricl as ( - +, ricl as ( select * from {{ ref('stg_rxnorm__ingredient_component_links') }} +) -), - -ric as ( - +, ric as ( select * from {{ ref('stg_rxnorm__ingredient_components') }} - ) select diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_strengths.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_strengths.sql index 9857b848..d899ee1c 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_strengths.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredient_strengths.sql @@ -1,71 +1,47 @@ -- int_rxnorm_clinical_products_to_ingredient_strengths.sql -with - -rcp as ( - +with rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rcpcl as ( - +, rcpcl as ( select * from {{ ref('stg_rxnorm__clinical_product_component_links') }} +) -), - -rcpc as ( - +, rcpc as ( select * from {{ ref('stg_rxnorm__clinical_product_components') }} +) -), - -rdf as ( - +, rdf as ( select * from {{ ref('stg_rxnorm__dose_forms') }} +) -), - -ri as ( - +, ri as ( select * from {{ ref('stg_rxnorm__ingredients') }} +) -), - -ricl as ( - +, ricl as ( select * from {{ ref('stg_rxnorm__ingredient_component_links') }} +) -), - -ric as ( - +, ric as ( select * from {{ ref('stg_rxnorm__ingredient_components') }} +) -), - -risl as ( - +, risl as ( select * from {{ ref('stg_rxnorm__ingredient_strength_links') }} +) -), - -ris as ( - +, ris as ( select * from {{ ref('stg_rxnorm__ingredient_strengths') }} +) -), - -pinl as ( - +, pinl as ( select * from {{ ref('stg_rxnorm__precise_ingredient_links') }} +) -), - -pin as ( - +, pin as ( select * from {{ ref('stg_rxnorm__precise_ingredients') }} - ) select diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredients.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredients.sql index 76820dd9..e8a7a8ac 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredients.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ingredients.sql @@ -1,35 +1,23 @@ -- int_rxnorm_clinical_products_to_ingredients.sql -with - -rcp as ( - +with rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rcpcl as ( - +, rcpcl as ( select * from {{ ref('stg_rxnorm__clinical_product_component_links') }} +) -), - -rcpc as ( - +, rcpc as ( select * from {{ ref('stg_rxnorm__clinical_product_components') }} +) -), - -rdf as ( - +, rdf as ( select * from {{ ref('stg_rxnorm__dose_forms') }} +) -), - -ri as ( - +, ri as ( select * from {{ ref('stg_rxnorm__ingredients') }} - ) select diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ndcs.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ndcs.sql index 5e29db9d..74d49a2f 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ndcs.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_clinical_products_to_ndcs.sql @@ -1,41 +1,28 @@ -- int_rxnorm_clinical_products_to_ndcs.sql -with - -rcp as ( +with rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rcpcl as ( - +, rcpcl as ( select * from {{ ref('stg_rxnorm__clinical_product_component_links') }} +) -), - -rcpc as ( - +, rcpc as ( select * from {{ ref('stg_rxnorm__clinical_product_components') }} +) -), - -rdf as ( - +, rdf as ( select * from {{ ref('stg_rxnorm__dose_forms') }} +) -), - -ri as ( - +, ri as ( select * from {{ ref('stg_rxnorm__ingredients') }} +) -), - -rn as ( - +, rn as ( select * from {{ ref('stg_rxnorm__ndcs') }} - ) select diff --git a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_ndcs_to_products.sql b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_ndcs_to_products.sql index f73a9227..8e1a53f3 100644 --- a/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_ndcs_to_products.sql +++ b/dbt/sagerx/models/intermediate/rxnorm/int_rxnorm_ndcs_to_products.sql @@ -1,23 +1,15 @@ -- int_rxnorm_ndcs_to_products.sql -with - -ndc as ( - +with ndc as ( select * from {{ ref('stg_rxnorm__ndcs') }} +) -), - -rcp as ( - +, rcp as ( select * from {{ ref('stg_rxnorm__clinical_products') }} +) -), - -rbp as ( - +, rbp as ( select * from {{ ref('stg_rxnorm__brand_products') }} - ) select distinct diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__all_ndcs.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__all_ndcs.sql index 1da0384d..39dbc28e 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__all_ndcs.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__all_ndcs.sql @@ -1,12 +1,38 @@ -- stg_rxnorm__all_ndcs.sql - -select - {{ ndc_to_11 ('rxnsat.atv') }}as ndc11 - , rxnsat.atv as ndc - , rxnsat.rxcui - , rxnsat.sab - , case when rxnsat.suppress = 'N' then true else false end as active - , case when rxnsat.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnsat rxnsat - where rxnsat.atn = 'NDC' - and rxnsat.sab in ('ATC', 'CVX', 'DRUGBANK', 'MSH', 'MTHCMSFRF', 'MTHSPL', 'RXNORM', 'USP', 'VANDF') +WITH rxnsat AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnsat' + ) }} +) +SELECT + {{ ndc_to_11 ('rxnsat.atv') }} AS ndc11, + rxnsat.atv AS ndc, + rxnsat.rxcui, + rxnsat.sab, + CASE + WHEN rxnsat.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN rxnsat.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + rxnsat +WHERE + rxnsat.atn = 'NDC' + AND rxnsat.sab IN ( + 'ATC', + 'CVX', + 'DRUGBANK', + 'MSH', + 'MTHCMSFRF', + 'MTHSPL', + 'RXNORM', + 'USP', + 'VANDF' + ) diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__atc_codes.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__atc_codes.sql index 0afb7a6d..98b64cff 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__atc_codes.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__atc_codes.sql @@ -1,86 +1,116 @@ -with atc as ( - select distinct a.rxcui - ,a.code - ,b.atn - ,b.atv as atc_class_level - ,a.str as description - ,a.sab - ,a.tty - from ( - select * - from sagerx_lake.rxnorm_rxnconso - where sab = 'ATC' - and tty not like 'RXN%' - order by code - ) a - left join sagerx_lake.rxnorm_rxnsat b - on a.code= b.code - where atn = 'ATC_LEVEL' - order by code +WITH atc AS ( + SELECT + DISTINCT A.rxcui, + A.code, + b.atn, + b.atv AS atc_class_level, + A.str AS description, + A.sab, + A.tty + FROM + ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} + WHERE + sab = 'ATC' + AND tty NOT LIKE 'RXN%' + ORDER BY + code + ) A + LEFT JOIN {{ source( + 'rxnorm', + 'rxnorm_rxnsat' + ) }} AS b + ON A.code = b.code + WHERE + atn = 'ATC_LEVEL' + ORDER BY + code +), +atc_5 AS ( + SELECT + * + FROM + atc + WHERE + atc_class_level = '5' +), +atc_4 AS ( + SELECT + * + FROM + atc + WHERE + atc_class_level = '4' +), +atc_3 AS ( + SELECT + * + FROM + atc + WHERE + atc_class_level = '3' +), +atc_2 AS ( + SELECT + * + FROM + atc + WHERE + atc_class_level = '2' +), +atc_1 AS ( + SELECT + * + FROM + atc + WHERE + atc_class_level = '1' +), +sagerx_atc AS ( + SELECT + atc_1.code AS atc_1_code, + atc_1.description AS atc_1_name, + atc_2.code AS atc_2_code, + atc_2.description AS atc_2_name, + atc_3.code AS atc_3_code, + atc_3.description AS atc_3_name, + atc_4.code AS atc_4_code, + atc_4.description AS atc_4_name, + atc_5.code AS atc_5_code, + atc_5.description AS atc_5_name, + atc_5.rxcui AS ingredient_rxcui, + atc_5.description AS ingredient_name, + atc_5.tty AS ingredient_tty + FROM + atc_5 + LEFT JOIN atc_4 + ON LEFT( + atc_5.code, + 5 + ) = atc_4.code + LEFT JOIN atc_3 + ON LEFT( + atc_4.code, + 4 + ) = atc_3.code + LEFT JOIN atc_2 + ON LEFT( + atc_3.code, + 3 + ) = atc_2.code + LEFT JOIN atc_1 + ON LEFT( + atc_2.code, + 1 + ) = atc_1.code ) - -, atc_5 as ( - select - * - from atc - where atc_class_level = '5' -) - -, atc_4 as ( - select - * - from atc - where atc_class_level = '4' -) - -, atc_3 as ( - select - * - from atc - where atc_class_level = '3' -) - -, atc_2 as ( - select - * - from atc - where atc_class_level = '2' -) - -, atc_1 as ( - select - * - from atc - where atc_class_level = '1' -) - -, sagerx_atc as ( - -select - atc_1.code as atc_1_code - , atc_1.description as atc_1_name - , atc_2.code as atc_2_code - , atc_2.description as atc_2_name - , atc_3.code as atc_3_code - , atc_3.description as atc_3_name - , atc_4.code as atc_4_code - , atc_4.description as atc_4_name - , atc_5.code as atc_5_code - , atc_5.description as atc_5_name - , atc_5.rxcui as ingredient_rxcui - , atc_5.description as ingredient_name - , atc_5.tty as ingredient_tty - -from atc_5 -left join atc_4 - on left(atc_5.code, 5) = atc_4.code -left join atc_3 - on left(atc_4.code, 4) = atc_3.code -left join atc_2 - on left(atc_3.code, 3) = atc_2.code -left join atc_1 - on left(atc_2.code, 1) = atc_1.code -) - -select * -from sagerx_atc +SELECT + * +FROM + sagerx_atc diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_component_links.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_component_links.sql index b8036510..c5cbf7fb 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_component_links.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_component_links.sql @@ -1,16 +1,52 @@ -- stg_rxnorm__brand_product_component_links.sql - -select distinct - product.rxcui as brand_product_rxcui - , case when product_component.rxcui is null - then product.rxcui - else product_component.rxcui - end as brand_product_component_rxcui -from sagerx_lake.rxnorm_rxnconso product -left join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = product.rxcui and rxnrel.rela = 'contains' -left join sagerx_lake.rxnorm_rxnconso product_component - on rxnrel.rxcui1 = product_component.rxcui - and product_component.tty in ('SBD', 'SCD') -- NOTE: BPCKs can contain SBDs AND SCDs - and product_component.sab = 'RXNORM' -where product.tty in ('SBD', 'BPCK') - and product.sab = 'RXNORM' +WITH product AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +product_component AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + DISTINCT product.rxcui AS brand_product_rxcui, + CASE + WHEN product_component.rxcui IS NULL THEN product.rxcui + ELSE product_component.rxcui + END AS brand_product_component_rxcui +FROM + product + LEFT JOIN rxnrel + ON rxnrel.rxcui2 = product.rxcui + AND rxnrel.rela = 'contains' + LEFT JOIN product_component + ON rxnrel.rxcui1 = product_component.rxcui + AND product_component.tty IN ( + 'SBD', + 'SCD' + ) -- NOTE: BPCKs can contain SBDs AND SCDs + AND product_component.sab = 'RXNORM' +WHERE + product.tty IN ( + 'SBD', + 'BPCK' + ) + AND product.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_components.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_components.sql index 8b00d0e0..64161506 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_components.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_product_components.sql @@ -1,38 +1,90 @@ -- stg_rxnorm__brand_product_components.sql - -select distinct - case when product.tty = 'SBD' then product.rxcui else product_component.rxcui end rxcui - , case when product.tty = 'SBD' then product.str else product_component.str end name - , case when product.tty = 'SBD' then product.tty else product_component.tty end tty - , case when product_component.tty = 'SCD' then product_component.rxcui else rxnrel_scd.rxcui1 end clinical_product_component_rxcui - , rxnrel_bn.rxcui1 as brand_rxcui - , case when - case when product.tty = 'SBD' - then product.suppress - else product_component.suppress - end = 'N' - then true - else false - end as active - , case when - case when product.tty = 'SBD' - then product.cvf - else product_component.cvf - end = '4096' - then true - else false - end as prescribable -from sagerx_lake.rxnorm_rxnconso product -left join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = product.rxcui and rxnrel.rela = 'contains' -left join sagerx_lake.rxnorm_rxnconso product_component - on rxnrel.rxcui1 = product_component.rxcui - and product_component.tty in ('SBD', 'SCD') -- NOTE: BPCKs can contain SBDs AND SCDs - and product_component.sab = 'RXNORM' -left join sagerx_lake.rxnorm_rxnrel rxnrel_scd - on rxnrel_scd.rxcui2 = case when product_component.rxcui is null then product.rxcui else product_component.rxcui end - and rxnrel_scd.rela = 'tradename_of' -- rxnrel_scd.rxcui1 = clinical_product_component_rxcui -left join sagerx_lake.rxnorm_rxnrel rxnrel_bn - on rxnrel_bn.rxcui2 = case when product_component.rxcui is null then product.rxcui else product_component.rxcui end - and rxnrel_bn.rela = 'has_ingredient' -- rxnrel_bn.rxcui1 = brand_rxcui -where product.tty in ('SBD', 'BPCK') - and product.sab = 'RXNORM' +WITH product AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +product_component AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + DISTINCT CASE + WHEN product.tty = 'SBD' THEN product.rxcui + ELSE product_component.rxcui + END rxcui, + CASE + WHEN product.tty = 'SBD' THEN product.str + ELSE product_component.str + END NAME, + CASE + WHEN product.tty = 'SBD' THEN product.tty + ELSE product_component.tty + END tty, + CASE + WHEN product_component.tty = 'SCD' THEN product_component.rxcui + ELSE rxnrel_scd.rxcui1 + END clinical_product_component_rxcui, + rxnrel_bn.rxcui1 AS brand_rxcui, + CASE + WHEN CASE + WHEN product.tty = 'SBD' THEN product.suppress + ELSE product_component.suppress + END = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN CASE + WHEN product.tty = 'SBD' THEN product.cvf + ELSE product_component.cvf + END = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + product + LEFT JOIN rxnrel + ON rxnrel.rxcui2 = product.rxcui + AND rxnrel.rela = 'contains' + LEFT JOIN product_component + ON rxnrel.rxcui1 = product_component.rxcui + AND product_component.tty IN ( + 'SBD', + 'SCD' + ) -- NOTE: BPCKs can contain SBDs AND SCDs + AND product_component.sab = 'RXNORM' + LEFT JOIN rxnrel AS rxnrel_scd + ON rxnrel_scd.rxcui2 = CASE + WHEN product_component.rxcui IS NULL THEN product.rxcui + ELSE product_component.rxcui + END + AND rxnrel_scd.rela = 'tradename_of' -- rxnrel_scd.rxcui1 = clinical_product_component_rxcui + LEFT JOIN rxnrel AS rxnrel_bn + ON rxnrel_bn.rxcui2 = CASE + WHEN product_component.rxcui IS NULL THEN product.rxcui + ELSE product_component.rxcui + END + AND rxnrel_bn.rela = 'has_ingredient' -- rxnrel_bn.rxcui1 = brand_rxcui +WHERE + product.tty IN ( + 'SBD', + 'BPCK' + ) + AND product.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_products.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_products.sql index 6e03887c..a0459fad 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_products.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brand_products.sql @@ -1,17 +1,59 @@ -- stg_rxnorm__brand_products.sql - -select - product.rxcui as rxcui - , product.str as name - , product.tty as tty - , clinical_product.rxcui as clinical_product_rxcui - , case when product.suppress = 'N' then true else false end as active - , case when product.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso product -left join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = product.rxcui and rxnrel.rela = 'tradename_of' -left join sagerx_lake.rxnorm_rxnconso clinical_product - on rxnrel.rxcui1 = clinical_product.rxcui - and clinical_product.tty in ('SCD', 'GPCK') - and clinical_product.sab = 'RXNORM' -where product.tty in('SBD', 'BPCK') - and product.sab = 'RXNORM' +WITH product AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +product_component AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + product.rxcui AS rxcui, + product.str AS NAME, + product.tty AS tty, + product_component.rxcui AS clinical_product_rxcui, + CASE + WHEN product.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN product.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + product + LEFT JOIN rxnrel + ON rxnrel.rxcui2 = product.rxcui + AND rxnrel.rela = 'tradename_of' + LEFT JOIN product_component + ON rxnrel.rxcui1 = product_component.rxcui + AND product_component.tty IN ( + 'SCD', + 'GPCK' + ) + AND product_component.sab = 'RXNORM' +WHERE + product.tty IN( + 'SBD', + 'BPCK' + ) + AND product.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brands.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brands.sql index 12a01c16..8b36e0d6 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brands.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__brands.sql @@ -1,62 +1,113 @@ -- stg_rxnorm__brands.sql - -with cte as ( - select - sq.* - , row_number() over(partition by rxcui order by ingredient_tty desc) as rn - from ( - - select - brand.rxcui as rxcui - , brand.str as name - , brand.tty as tty - , ingredient.rxcui as ingredient_rxcui - , ingredient.str as ingredient_name - , ingredient.tty as ingredient_tty - from sagerx_lake.rxnorm_rxnconso brand - inner join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = brand.rxcui and rxnrel.rela = 'tradename_of' - inner join sagerx_lake.rxnorm_rxnconso ingredient - on rxnrel.rxcui1 = ingredient.rxcui - and ingredient.tty = 'IN' - and ingredient.sab = 'RXNORM' - where brand.tty = 'BN' - and brand.sab = 'RXNORM' - - union all - - select - brand.rxcui as rxcui - , brand.str as name - , brand.tty as tty - , ingredient.rxcui as ingredient_rxcui - , ingredient.str as ingredient_name - , ingredient.tty as ingredient_tty - from sagerx_lake.rxnorm_rxnconso brand - inner join sagerx_lake.rxnorm_rxnrel sbd_rxnrel on sbd_rxnrel.rxcui2 = brand.rxcui and sbd_rxnrel.rela = 'ingredient_of' - inner join sagerx_lake.rxnorm_rxnrel scd_rxnrel on scd_rxnrel.rxcui2 = sbd_rxnrel.rxcui1 and scd_rxnrel.rela = 'tradename_of' - inner join sagerx_lake.rxnorm_rxnrel ingredient_rxnrel on ingredient_rxnrel.rxcui2 = scd_rxnrel.rxcui1 and ingredient_rxnrel.rela = 'has_ingredients' - left join sagerx_lake.rxnorm_rxnconso ingredient - on ingredient_rxnrel.rxcui1 = ingredient.rxcui - and ingredient.tty = 'MIN' - and ingredient.sab = 'RXNORM' - where brand.tty = 'BN' - and brand.sab = 'RXNORM' - ) sq +WITH brand AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +ingredient AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +cte AS ( + SELECT + sq.*, + ROW_NUMBER() over( + PARTITION BY rxcui + ORDER BY + ingredient_tty DESC + ) AS rn + FROM + ( + SELECT + brand.rxcui AS rxcui, + brand.str AS NAME, + brand.tty AS tty, + ingredient.rxcui AS ingredient_rxcui, + ingredient.str AS ingredient_name, + ingredient.tty AS ingredient_tty + FROM + brand + INNER JOIN rxnrel + ON rxnrel.rxcui2 = brand.rxcui + AND rxnrel.rela = 'tradename_of' + INNER JOIN ingredient + ON rxnrel.rxcui1 = ingredient.rxcui + AND ingredient.tty = 'IN' + AND ingredient.sab = 'RXNORM' + WHERE + brand.tty = 'BN' + AND brand.sab = 'RXNORM' + UNION ALL + SELECT + brand.rxcui AS rxcui, + brand.str AS NAME, + brand.tty AS tty, + ingredient.rxcui AS ingredient_rxcui, + ingredient.str AS ingredient_name, + ingredient.tty AS ingredient_tty + FROM + brand + INNER JOIN rxnrel AS sbd_rxnrel + ON sbd_rxnrel.rxcui2 = brand.rxcui + AND sbd_rxnrel.rela = 'ingredient_of' + INNER JOIN rxnrel AS scd_rxnrel + ON scd_rxnrel.rxcui2 = sbd_rxnrel.rxcui1 + AND scd_rxnrel.rela = 'tradename_of' + INNER JOIN rxnrel AS ingredient_rxnrel + ON ingredient_rxnrel.rxcui2 = scd_rxnrel.rxcui1 + AND ingredient_rxnrel.rela = 'has_ingredients' + LEFT JOIN ingredient + ON ingredient_rxnrel.rxcui1 = ingredient.rxcui + AND ingredient.tty = 'MIN' + AND ingredient.sab = 'RXNORM' + WHERE + brand.tty = 'BN' + AND brand.sab = 'RXNORM' + ) sq ) - -select distinct - brand.rxcui as rxcui - , brand.str as name - , brand.tty as tty - , case when brand.suppress = 'N' then true else false end as active - , case when brand.cvf = '4096' then true else false end as prescribable - , cte.ingredient_rxcui as ingredient_rxcui -from sagerx_lake.rxnorm_rxnconso product -inner join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = product.rxcui and rxnrel.rela = 'has_ingredient' -inner join sagerx_lake.rxnorm_rxnconso brand - on rxnrel.rxcui1 = brand.rxcui - and brand.tty = 'BN' - and brand.sab = 'RXNORM' -Left join cte on cte.rxcui = brand.rxcui and cte.rn < 2 -where product.tty = 'SBD' - and product.sab = 'RXNORM' +SELECT + DISTINCT brand.rxcui AS rxcui, + brand.str AS NAME, + brand.tty AS tty, + CASE + WHEN brand.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN brand.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable, + cte.ingredient_rxcui AS ingredient_rxcui +FROM + brand AS product + INNER JOIN rxnrel + ON rxnrel.rxcui2 = product.rxcui + AND rxnrel.rela = 'has_ingredient' + INNER JOIN brand + ON rxnrel.rxcui1 = brand.rxcui + AND brand.tty = 'BN' + AND brand.sab = 'RXNORM' + LEFT JOIN cte + ON cte.rxcui = brand.rxcui + AND cte.rn < 2 +WHERE + product.tty = 'SBD' + AND product.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_component_links.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_component_links.sql index e7950188..1871eb10 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_component_links.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_component_links.sql @@ -1,16 +1,44 @@ -- stg_rxnorm__clinical_product_component_links.sql -select distinct - product.rxcui as clinical_product_rxcui - , case when product_component.rxcui is null - then product.rxcui - else product_component.rxcui - end as clinical_product_component_rxcui -from sagerx_lake.rxnorm_rxnconso product -left join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = product.rxcui and rxnrel.rela = 'contains' -left join sagerx_lake.rxnorm_rxnconso product_component - on rxnrel.rxcui1 = product_component.rxcui - and product_component.tty = 'SCD' - and product_component.sab = 'RXNORM' -where product.tty in('SCD', 'GPCK') - and product.sab = 'RXNORM' +with + +clinical_products as ( + select + * + from {{ ref('stg_rxnorm__clinical_products') }} +), + +rxnrel as ( + + select + rxcui2 as clinical_product_rxcui, + rxcui1 as clinical_product_component_rxcui + + from + {{ ref( + 'stg_rxnorm__rxnrel_ttys' + ) }} + + where tty2 = 'GPCK' + and rela = 'contains' + and tty1 = 'SCD' + +), + +clinical_product_component_links as ( + + select + clinical_products.rxcui as clinical_product_rxcui, + case + when clinical_product_component_rxcui is not null + then clinical_product_component_rxcui + else clinical_products.rxcui + end as clinical_product_component_rxcui + from clinical_products + left join rxnrel + on clinical_product_rxcui + = clinical_products.rxcui + +) + +select * from clinical_product_component_links diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_components.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_components.sql index e057a743..da771daa 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_components.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_product_components.sql @@ -1,74 +1,152 @@ -- stg_rxnorm__clinical_product_components.sql +with + +clinical_product_components as ( + + select + rxcui, + str as name, + tty + + from + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} + + where sab = 'RXNORM' and + tty = 'SCD' + +), + +rxnrel as ( + + select + * + + from + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} + + where rela in ( + 'consists_of', + 'has_ingredients', + 'has_ingredient' + ) + +), + +ingredients AS ( + + select + rxcui, + str as name, + tty + + from + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} + + where sab = 'RXNORM' and + tty in ( + 'IN', + 'MIN' + ) + +), + +cpc_to_multiple_ingredients as ( + + select + cpc.rxcui as clinical_product_component_rxcui, + rxnrel.rxcui1 as ingredient_rxcui, + + from + clinical_product_components cpc + + inner join rxnrel + on rxnrel.rxcui2 = cpc.rxcui and + rxnrel.rela = 'has_ingredients' + +), + +cpc_to_single_ingredients as ( + + select + cpc.rxcui + as clinical_product_component_rxcui, + ingredient_rxnrel.rxcui1 as ingredient_rxcui, + + from + clinical_product_components cpc + + inner join rxnrel as scdc_rxnrel + on scdc_rxnrel.rxcui2 = cpc.rxcui and + scdc_rxnrel.rela = 'consists_of' + inner join rxnrel as ingredient_rxnrel + on ingredient_rxnrel.rxcui2 = scdc_rxnrel.rxcui1 and + ingredient_rxnrel.rela = 'has_ingredient' + +), + +cpc_to_all_ingredients as ( + + select + * + from clinical_products_to_multiple_ingredients + + union all + + select + * + from clinical_products_to_single_ingredients -with cte as ( - select - sq.*, - row_number() over(partition by product_component_rxcui order by ingredient_tty desc) as rn - from ( - select - product_component.rxcui as product_component_rxcui - , product_component.str as product_component_name - , product_component.tty as product_component_tty - , ingredient.rxcui as ingredient_rxcui - , ingredient.str as ingredient_name - , ingredient.tty as ingredient_tty - from sagerx_lake.rxnorm_rxnconso product_component - inner join sagerx_lake.rxnorm_rxnrel rxnrel - on rxnrel.rxcui2 = product_component.rxcui and rxnrel.rela = 'has_ingredients' - inner join sagerx_lake.rxnorm_rxnconso ingredient - on rxnrel.rxcui1 = ingredient.rxcui - and ingredient.tty = 'MIN' - and ingredient.sab = 'RXNORM' - where product_component.tty = 'SCD' - and product_component.sab = 'RXNORM' - - union all - - select - product_component.rxcui as product_component_rxcui - , product_component.str as product_component_name - , product_component.tty as product_component_tty - , ingredient.rxcui as ingredient_rxcui - , ingredient.str as ingredient_name - , ingredient.tty as ingredient_tty - from sagerx_lake.rxnorm_rxnconso product_component - inner join sagerx_lake.rxnorm_rxnrel scdc_rxnrel - on scdc_rxnrel.rxcui2 = product_component.rxcui and scdc_rxnrel.rela = 'consists_of' - inner join sagerx_lake.rxnorm_rxnconso scdc - on scdc_rxnrel.rxcui1 = scdc.rxcui - inner join sagerx_lake.rxnorm_rxnrel ingredient_rxnrel - on ingredient_rxnrel.rxcui2 = scdc.rxcui and ingredient_rxnrel.rela = 'has_ingredient' - inner join sagerx_lake.rxnorm_rxnconso ingredient - on ingredient_rxnrel.rxcui1 = ingredient.rxcui - and ingredient.tty = 'IN' - and ingredient.sab = 'RXNORM' - where product_component.tty = 'SCD' - and product_component.sab = 'RXNORM' - ) sq ) -select distinct - case when product_component.rxcui is null then product.rxcui else product_component.rxcui end rxcui - , case when product_component.str is null then product.str else product_component.str end name - , case when product_component.tty is null then product.tty else product_component.tty end tty - , case when - case when product_component.rxcui is null then product.suppress else product_component.suppress end = 'N' then true else false end as active - , case when - case when product_component.rxcui is null then product.cvf else product_component.cvf end = '4096' then true else false end as prescribable - , cte.ingredient_rxcui as ingredient_rxcui - , dose_form_rxnrel.rxcui1 as dose_form_rxcui -from sagerx_lake.rxnorm_rxnconso product -left join sagerx_lake.rxnorm_rxnrel rxnrel on rxnrel.rxcui2 = product.rxcui and rxnrel.rela = 'contains' -left join sagerx_lake.rxnorm_rxnconso product_component - on rxnrel.rxcui1 = product_component.rxcui - and product_component.tty = 'SCD' - and product_component.sab = 'RXNORM' -left join cte - on cte.product_component_rxcui = case when product_component.rxcui is null then product.rxcui else product_component.rxcui end - and cte.rn < 2 -left join sagerx_lake.rxnorm_rxnrel dose_form_rxnrel - on dose_form_rxnrel.rxcui2 = case when product_component.rxcui is null then product.rxcui else product_component.rxcui end - and dose_form_rxnrel.rela = 'has_dose_form' - and dose_form_rxnrel.sab = 'RXNORM' -where product.tty in('SCD', 'GPCK') - and product.sab = 'RXNORM' + +ranked_clinical_product_component_ingredients as ( + + select + *, + row_number() over( + partition by clinical_product_component_rxcui + order by + ingredient_tty desc + ) as rn + from cpc_to_all_ingredients + +) +select + rxcui, + name, + tty, + {{ active_and_prescribable() }}, + cte.ingredient_rxcui AS ingredient_rxcui, + dose_form_rxnrel.rxcui1 as dose_form_rxcui +from + clinical_product_components + /* + left join ingredients + on ingredients.clinical_product_component_rxcui = + clinical_product_components.rxcui + left join dose_forms + on dose_forms.clinical_product_component_rxcui = + clinical_product_components.rxcui + */ + left join cte + on cte.product_component_rxcui = case + when product_component.rxcui is null then product.rxcui + else product_component.rxcui + end + and cte.rn < 2 + left join rxnrel as dose_form_rxnrel + on dose_form_rxnrel.rxcui2 = case + when product_component.rxcui is null then product.rxcui + else product_component.rxcui + end + and dose_form_rxnrel.rela = 'has_dose_form' + and dose_form_rxnrel.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_products.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_products.sql index 74f6eddf..c78d4460 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_products.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__clinical_products.sql @@ -1,11 +1,28 @@ -- stg_rxnorm__clinical_products.sql -select - product.rxcui as rxcui - , product.str as name - , product.tty as tty - , case when product.suppress = 'N' then true else false end as active - , case when product.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso product -where product.tty in('SCD', 'GPCK') - and product.sab = 'RXNORM' +with + +clinical_products as ( + + select + rxcui, + str as name, + tty, + {{ active_and_prescribable() }} + + from + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} + + where + tty in ( + 'SCD', + 'GPCK' + ) and + sab = 'RXNORM' + +) + +select * from clinical_products diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__common_ingredient_component.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__common_ingredient_component.sql new file mode 100644 index 00000000..800da0c0 --- /dev/null +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__common_ingredient_component.sql @@ -0,0 +1,54 @@ +-- stg_rxnorm__common_ingredient_component.sql +WITH rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +ingredient_component AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +cte AS ( + SELECT + rxnrel.rxcui2 AS ingredient_rxcui, + ingredient_component.rxcui AS rxcui, + ingredient_component.str AS NAME, + ingredient_component.tty AS tty, + ingredient_component.suppress, + ingredient_component.cvf + FROM + rxnrel + INNER JOIN ingredient_component + ON rxnrel.rxcui1 = ingredient_component.rxcui + WHERE + rxnrel.rela = 'hAS_part' + AND ingredient_component.tty = 'IN' + AND ingredient_component.sab = 'RXNORM' +) +SELECT + ingredient_component.*, + cte.ingredient_rxcui, + cte.rxcui AS ingredient_component_rxcui, + cte.name AS ingredient_component_name, + cte.tty AS ingredient_component_tty, + cte.suppress AS ingredient_component_suppress, + cte.cvf AS ingredient_component_cvf +FROM + ingredient_component + LEFT JOIN cte + ON ingredient_component.rxcui = cte.ingredient_rxcui +WHERE + ingredient_component.tty IN ( + 'IN', + 'MIN' + ) + AND ingredient_component.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_group_links.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_group_links.sql index 72a30344..e25bc2aa 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_group_links.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_group_links.sql @@ -1,12 +1,31 @@ -- stg_rxnorm__dose_form_group_links.sql - -select distinct - dose_form.rxcui dose_form_rxcui - , rxnrel.rxcui1 dose_form_group_rxcui -from sagerx_lake.rxnorm_rxnconso dose_form -inner join sagerx_lake.rxnorm_rxnrel rxnrel - on rxnrel.rxcui2 = dose_form.rxcui - and rxnrel.rela = 'isa' - and rxnrel.sab = 'RXNORM' -where dose_form.tty = 'DF' - and dose_form.sab = 'RXNORM' +WITH rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +dose_form AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + DISTINCT dose_form.rxcui dose_form_rxcui, + rxnrel.rxcui1 dose_form_group_rxcui +FROM + dose_form + INNER JOIN rxnrel + ON rxnrel.rxcui2 = dose_form.rxcui + AND rxnrel.rela = 'isa' + AND rxnrel.sab = 'RXNORM' +WHERE + dose_form.tty = 'DF' + AND dose_form.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_groups.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_groups.sql index f4d68e70..a75b5fb0 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_groups.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_form_groups.sql @@ -1,11 +1,27 @@ -- stg_rxnorm__dose_form_groups.sql - -select - dose_form_group.rxcui rxcui - , dose_form_group.str name - , dose_form_group.tty tty - , case when dose_form_group.suppress = 'N' then true else false end as active - , case when dose_form_group.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso dose_form_group -where dose_form_group.tty = 'DFG' - and dose_form_group.sab = 'RXNORM' +WITH dose_form_group AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + dose_form_group.rxcui rxcui, + dose_form_group.str NAME, + dose_form_group.tty tty, + CASE + WHEN dose_form_group.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN dose_form_group.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + dose_form_group +WHERE + dose_form_group.tty = 'DFG' + AND dose_form_group.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_forms.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_forms.sql index efe4ba34..26fad5c6 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_forms.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__dose_forms.sql @@ -1,11 +1,27 @@ -- stg_rxnorm__dose_forms.sql - -select - dose_form.rxcui rxcui - , dose_form.str name - , dose_form.tty tty - , case when dose_form.suppress = 'N' then true else false end as active - , case when dose_form.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso dose_form -where dose_form.tty = 'DF' - and dose_form.sab = 'RXNORM' +WITH dose_form AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + dose_form.rxcui rxcui, + dose_form.str NAME, + dose_form.tty tty, + CASE + WHEN dose_form.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN dose_form.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + dose_form +WHERE + dose_form.tty = 'DF' + AND dose_form.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_component_links.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_component_links.sql index 47e3cfd2..bfb54cac 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_component_links.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_component_links.sql @@ -1,27 +1,15 @@ -- stg_rxnorm__ingredient_component_links.sql - -with cte as ( - select - rxnrel.rxcui2 as ingredient_rxcui - , ingredient_component.rxcui as ingredient_component_rxcui - , ingredient_component.str as ingredient_component_name - , ingredient_component.tty as ingredient_component_tty - from - sagerx_lake.rxnorm_rxnrel rxnrel - inner join sagerx_lake.rxnorm_rxnconso ingredient_component - on rxnrel.rxcui1 = ingredient_component.rxcui - where rxnrel.rela = 'has_part' - and ingredient_component.tty = 'IN' - and ingredient_component.sab = 'RXNORM' +WITH cte AS ( + SELECT + * + FROM + {{ ref('stg_rxnorm__common_ingredient_component') }} ) - -select distinct - ingredient.rxcui as ingredient_rxcui - , case when cte.ingredient_component_rxcui is null - then ingredient.rxcui - else cte.ingredient_component_rxcui - end as ingredient_component_rxcui -from sagerx_lake.rxnorm_rxnconso ingredient -left join cte on ingredient.rxcui = cte.ingredient_rxcui -where ingredient.tty in('IN', 'MIN') - and ingredient.sab = 'RXNORM' +SELECT + DISTINCT cte.rxcui AS ingredient_rxcui, + CASE + WHEN cte.ingredient_component_rxcui IS NULL THEN cte.rxcui + ELSE cte.ingredient_component_rxcui + END AS ingredient_component_rxcui +FROM + cte diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_components.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_components.sql index e828d24c..45042fc6 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_components.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_components.sql @@ -1,31 +1,36 @@ -- stg_rxnorm__ingredient_components.sql - -with cte as ( - select - rxnrel.rxcui2 as ingredient_rxcui - , ingredient_component.rxcui as rxcui - , ingredient_component.str as name - , ingredient_component.tty as tty - , ingredient_component.suppress - , ingredient_component.cvf - from - sagerx_lake.rxnorm_rxnrel rxnrel - inner join sagerx_lake.rxnorm_rxnconso ingredient_component - on rxnrel.rxcui1 = ingredient_component.rxcui - where rxnrel.rela = 'has_part' - and ingredient_component.tty = 'IN' - and ingredient_component.sab = 'RXNORM' +WITH cte AS ( + SELECT + * + FROM + {{ ref('stg_rxnorm__common_ingredient_component') }} ) - -select distinct - case when cte.rxcui is null then ingredient.rxcui else cte.rxcui end rxcui - , case when cte.name is null then ingredient.str else cte.name end name - , case when cte.tty is null then ingredient.tty else cte.tty end tty - , case when - case when cte.rxcui is null then ingredient.suppress else cte.suppress end = 'N' then true else false end as active - , case when - case when cte.rxcui is null then ingredient.cvf else cte.cvf end = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso ingredient -left join cte on ingredient.rxcui = cte.ingredient_rxcui -where ingredient.tty in('IN', 'MIN') - and ingredient.sab = 'RXNORM' +SELECT + DISTINCT CASE + WHEN cte.ingredient_component_rxcui IS NULL THEN cte.rxcui + ELSE cte.ingredient_component_rxcui + END rxcui, + CASE + WHEN cte.ingredient_component_name IS NULL THEN cte.str + ELSE cte.ingredient_component_name + END NAME, + CASE + WHEN cte.ingredient_component_tty IS NULL THEN cte.tty + ELSE cte.ingredient_component_tty + END tty, + CASE + WHEN CASE + WHEN cte.ingredient_component_rxcui IS NULL THEN cte.suppress + ELSE cte.ingredient_component_suppress + END = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN CASE + WHEN cte.ingredient_component_rxcui IS NULL THEN cte.cvf + ELSE cte.ingredient_component_cvf + END = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + cte diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strength_links.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strength_links.sql index aeef43b6..25aff021 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strength_links.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strength_links.sql @@ -1,23 +1,42 @@ -- stg_rxnorm__ingredient_strength_links.sql - -select distinct - product_component.rxcui as clinical_product_component_rxcui - , ingredient.rxcui as ingredient_component_rxcui - , ingredient_strength.rxcui as ingredient_strength_rxcui -from sagerx_lake.rxnorm_rxnconso ingredient_strength -inner join sagerx_lake.rxnorm_rxnrel has_ingredient - on has_ingredient.rxcui2 = ingredient_strength.rxcui - and has_ingredient.rela = 'has_ingredient' -inner join sagerx_lake.rxnorm_rxnconso ingredient - on ingredient.rxcui = has_ingredient.rxcui1 - and ingredient.tty = 'IN' - and ingredient.sab = 'RXNORM' -inner join sagerx_lake.rxnorm_rxnrel constitutes - on constitutes.rxcui2 = ingredient_strength.rxcui - and constitutes.rela = 'constitutes' -inner join sagerx_lake.rxnorm_rxnconso product_component - on product_component.rxcui = constitutes.rxcui1 - and product_component.tty = 'SCD' - and product_component.sab = 'RXNORM' -where ingredient_strength.tty = 'SCDC' - and ingredient_strength.sab = 'RXNORM' +WITH rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +ingredient AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + DISTINCT product_component.rxcui AS clinical_product_component_rxcui, + ingredient.rxcui AS ingredient_component_rxcui, + ingredient_strength.rxcui AS ingredient_strength_rxcui +FROM + ingredient AS ingredient_strength + INNER JOIN rxnrel AS has_ingredient + ON has_ingredient.rxcui2 = ingredient_strength.rxcui + AND has_ingredient.rela = 'has_ingredient' + INNER JOIN ingredient + ON ingredient.rxcui = has_ingredient.rxcui1 + AND ingredient.tty = 'IN' + AND ingredient.sab = 'RXNORM' + INNER JOIN rxnrel AS constitutes + ON constitutes.rxcui2 = ingredient_strength.rxcui + AND constitutes.rela = 'constitutes' + INNER JOIN ingredient AS product_component + ON product_component.rxcui = constitutes.rxcui1 + AND product_component.tty = 'SCD' + AND product_component.sab = 'RXNORM' +WHERE + ingredient_strength.tty = 'SCDC' + AND ingredient_strength.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strengths.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strengths.sql index bd258b47..e58db9a7 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strengths.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredient_strengths.sql @@ -1,36 +1,55 @@ -- stg_rxnorm__ingredient_strengths.sql - -select - ingredient_strength.rxcui as rxcui - , ingredient_strength.str as name - , numerator_value.atv as numerator_value - , numerator_unit.atv as numerator_unit - , denominator_value.atv as denominator_value - , denominator_unit.atv as denominator_unit - , text.atv as text - , case when ingredient_strength.suppress = 'N' - then true - else false - end as active - , case when ingredient_strength.cvf = '4096' - then true - else false - end as prescribable -from sagerx_lake.rxnorm_rxnconso ingredient_strength -left join sagerx_lake.rxnorm_rxnsat numerator_value - on numerator_value.rxcui = ingredient_strength.rxcui - and numerator_value.atn = 'RXN_BOSS_STRENGTH_NUM_VALUE' -left join sagerx_lake.rxnorm_rxnsat numerator_unit - on numerator_unit.rxcui = ingredient_strength.rxcui - and numerator_unit.atn = 'RXN_BOSS_STRENGTH_NUM_UNIT' -left join sagerx_lake.rxnorm_rxnsat denominator_value - on denominator_value.rxcui = ingredient_strength.rxcui - and denominator_value.atn = 'RXN_BOSS_STRENGTH_DENOM_VALUE' -left join sagerx_lake.rxnorm_rxnsat denominator_unit - on denominator_unit.rxcui = ingredient_strength.rxcui - and denominator_unit.atn = 'RXN_BOSS_STRENGTH_DENOM_UNIT' -left join sagerx_lake.rxnorm_rxnsat text - on text.rxcui = ingredient_strength.rxcui - and text.atn = 'RXN_STRENGTH' -where ingredient_strength.tty = 'SCDC' - and ingredient_strength.sab = 'RXNORM' +WITH ingredient_strength AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +rxnsat AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnsat' + ) }} +) +SELECT + ingredient_strength.rxcui AS rxcui, + ingredient_strength.str AS NAME, + numerator_value.atv AS numerator_value, + numerator_unit.atv AS numerator_unit, + denominator_value.atv AS denominator_value, + denominator_unit.atv AS denominator_unit, + text.atv AS text, + CASE + WHEN ingredient_strength.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN ingredient_strength.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + ingredient_strength + LEFT JOIN rxnsat AS numerator_value + ON numerator_value.rxcui = ingredient_strength.rxcui + AND numerator_value.atn = 'RXN_BOSS_STRENGTH_NUM_VALUE' + LEFT JOIN rxnsat AS numerator_unit + ON numerator_unit.rxcui = ingredient_strength.rxcui + AND numerator_unit.atn = 'RXN_BOSS_STRENGTH_NUM_UNIT' + LEFT JOIN rxnsat AS denominator_value + ON denominator_value.rxcui = ingredient_strength.rxcui + AND denominator_value.atn = 'RXN_BOSS_STRENGTH_DENOM_VALUE' + LEFT JOIN rxnsat AS denominator_unit + ON denominator_unit.rxcui = ingredient_strength.rxcui + AND denominator_unit.atn = 'RXN_BOSS_STRENGTH_DENOM_UNIT' + LEFT JOIN rxnsat AS text + ON text.rxcui = ingredient_strength.rxcui + AND text.atn = 'RXN_STRENGTH' +WHERE + ingredient_strength.tty = 'SCDC' + AND ingredient_strength.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredients.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredients.sql index 69ccda06..494e571a 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredients.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ingredients.sql @@ -1,11 +1,24 @@ -- stg_rxnorm__ingredients.sql - -select - ingredient.rxcui rxcui - , ingredient.str name - , ingredient.tty tty - , case when ingredient.suppress = 'N' then true else false end as active - , case when ingredient.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso ingredient -where ingredient.tty in('IN', 'MIN') - and ingredient.sab = 'RXNORM' +SELECT + ingredient.rxcui rxcui, + ingredient.str NAME, + ingredient.tty tty, + CASE + WHEN ingredient.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN ingredient.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} AS ingredient +WHERE + ingredient.tty IN( + 'IN', + 'MIN' + ) + AND ingredient.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_ndcs.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_ndcs.sql index 78186a93..aff30879 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_ndcs.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_ndcs.sql @@ -1,11 +1,21 @@ -- stg_rxnorm__mthspl_ndcs.sql - -select - {{ ndc_to_11 ('rxnsat.atv')}} as ndc11 - , rxnsat.atv as ndc - , rxnsat.rxcui - , case when rxnsat.suppress = 'N' then true else false end as active - , case when rxnsat.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnsat rxnsat - where rxnsat.atn = 'NDC' - and rxnsat.sab = 'MTHSPL' +SELECT + {{ ndc_to_11 ('rxnsat.atv') }} AS ndc11, + rxnsat.atv AS ndc, + rxnsat.rxcui, + CASE + WHEN rxnsat.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN rxnsat.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnsat' + ) }} AS rxnsat +WHERE + rxnsat.atn = 'NDC' + AND rxnsat.sab = 'MTHSPL' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_products.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_products.sql index 1af18c4e..dfd4b4a9 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_products.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_products.sql @@ -1,13 +1,23 @@ -- stg_rxnorm__mthspl_products.sql - -select - product.rxcui as rxcui - , product.str as name - , product.tty as tty - , product.rxaui as rxaui - , product.code as ndc - , case when product.suppress = 'N' then true else false end as active - , case when product.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso product -where product.tty = 'DP' - and product.sab = 'MTHSPL' +SELECT + product.rxcui AS rxcui, + product.str AS NAME, + product.tty AS tty, + product.rxaui AS rxaui, + product.code AS ndc, + CASE + WHEN product.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN product.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} AS product +WHERE + product.tty = 'DP' + AND product.sab = 'MTHSPL' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_substances.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_substances.sql index f4e89e60..c11e3734 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_substances.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__mthspl_substances.sql @@ -1,13 +1,23 @@ -- stg_rxnorm__mthspl_substances.sql - -select - substance.rxcui rxcui - , substance.str name - , substance.tty tty - , substance.rxaui rxaui - , substance.code unii - , case when substance.suppress = 'N' then true else false end as active - , case when substance.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso substance -where substance.tty = 'SU' - and substance.sab = 'MTHSPL' +SELECT + substance.rxcui rxcui, + substance.str NAME, + substance.tty tty, + substance.rxaui rxaui, + substance.code unii, + CASE + WHEN substance.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN substance.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} AS substance +WHERE + substance.tty = 'SU' + AND substance.sab = 'MTHSPL' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ndcs.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ndcs.sql index 1a8ab882..f9a6700a 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ndcs.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__ndcs.sql @@ -1,20 +1,97 @@ -- stg_rxnorm__ndcs.sql - -select rxnsat.atv as ndc - ,case when product.tty in ('BPCK','SBD') then clinical_product.rxcui - else rxnsat.rxcui end as clinical_product_rxcui - ,case when product.tty in ('BPCK','SBD') then rxnsat.rxcui - else null end as brand_product_rxcui - , case when rxnsat.suppress = 'N' then true else false end as active - , case when rxnsat.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnsat rxnsat - inner join sagerx_lake.rxnorm_rxnconso product on rxnsat.rxaui = product.rxaui - left join sagerx_lake.rxnorm_rxnrel rxnrel on rxnsat.rxcui = rxnrel.rxcui2 and rela = 'tradename_of' and product.tty in ('BPCK','SBD') - left join sagerx_lake.rxnorm_rxnconso clinical_product - on rxnrel.rxcui1 = clinical_product.rxcui - and clinical_product.tty in ('SCD','GPCK') - and clinical_product.sab = 'RXNORM' -where rxnsat.atn = 'NDC' - and rxnsat.sab in ('ATC', 'CVX', 'DRUGBANK', 'MSH', 'MTHCMSFRF', 'MTHSPL', 'RXNORM', 'USP', 'VANDF') - and product.tty in ('SCD','SBD','GPCK','BPCK') - and product.sab = 'RXNORM' +WITH rxnsat AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnsat' + ) }} +), +product AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +rxnrel AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +clinical_product AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + rxnsat.atv AS ndc,CASE + WHEN product.tty IN ( + 'BPCK', + 'SBD' + ) THEN clinical_product.rxcui + ELSE rxnsat.rxcui + END AS clinical_product_rxcui,CASE + WHEN product.tty IN ( + 'BPCK', + 'SBD' + ) THEN rxnsat.rxcui + ELSE NULL + END AS brand_product_rxcui, + CASE + WHEN rxnsat.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN rxnsat.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + rxnsat + INNER JOIN product + ON rxnsat.rxaui = product.rxaui + LEFT JOIN rxnrel + ON rxnsat.rxcui = rxnrel.rxcui2 + AND rela = 'tradename_of' + AND product.tty IN ( + 'BPCK', + 'SBD' + ) + LEFT JOIN clinical_product + ON rxnrel.rxcui1 = clinical_product.rxcui + AND clinical_product.tty IN ( + 'SCD', + 'GPCK' + ) + AND clinical_product.sab = 'RXNORM' +WHERE + rxnsat.atn = 'NDC' + AND rxnsat.sab IN ( + 'ATC', + 'CVX', + 'DRUGBANK', + 'MSH', + 'MTHCMSFRF', + 'MTHSPL', + 'RXNORM', + 'USP', + 'VANDF' + ) + AND product.tty IN ( + 'SCD', + 'SBD', + 'GPCK', + 'BPCK' + ) + AND product.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredient_links.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredient_links.sql index 808f38d0..e88ab6ba 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredient_links.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredient_links.sql @@ -1,15 +1,43 @@ -- stg_rxnorm__precise_ingredient_links.sql - -select distinct - ingredient_strength.rxcui as ingredient_strength_rxcui - , precise_ingredient.rxcui as precise_ingredient_rxcui -from sagerx_lake.rxnorm_rxnconso precise_ingredient -inner join sagerx_lake.rxnorm_rxnrel precise_ingredient_of - on precise_ingredient_of.rxcui2 = precise_ingredient.rxcui - and precise_ingredient_of.rela = 'precise_ingredient_of' -inner join sagerx_lake.rxnorm_rxnconso ingredient_strength - on ingredient_strength.rxcui = precise_ingredient_of.rxcui1 - and ingredient_strength.tty = 'SCDC' - and ingredient_strength.sab = 'RXNORM' -where precise_ingredient.tty = 'PIN' - and precise_ingredient.sab = 'RXNORM' +WITH precise_ingredient AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +), +precise_ingredient_of AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} +), +ingredient_strength AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + DISTINCT ingredient_strength.rxcui AS ingredient_strength_rxcui, + precise_ingredient.rxcui AS precise_ingredient_rxcui +FROM + precise_ingredient + INNER JOIN precise_ingredient_of + ON precise_ingredient_of.rxcui2 = precise_ingredient.rxcui + AND precise_ingredient_of.rela = 'precise_ingredient_of' + INNER JOIN ingredient_strength + ON ingredient_strength.rxcui = precise_ingredient_of.rxcui1 + AND ingredient_strength.tty = 'SCDC' + AND ingredient_strength.sab = 'RXNORM' +WHERE + precise_ingredient.tty = 'PIN' + AND precise_ingredient.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredients.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredients.sql index 05a56c2e..a157b1b6 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredients.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__precise_ingredients.sql @@ -1,11 +1,27 @@ -- stg_rxnorm__precise_ingredients.sql - -select - ingredient.rxcui rxcui - , ingredient.str name - , ingredient.tty tty - , case when ingredient.suppress = 'N' then true else false end as active - , case when ingredient.cvf = '4096' then true else false end as prescribable -from sagerx_lake.rxnorm_rxnconso ingredient -where ingredient.tty = 'PIN' - and ingredient.sab = 'RXNORM' +WITH ingredient AS ( + SELECT + * + FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +) +SELECT + ingredient.rxcui rxcui, + ingredient.str NAME, + ingredient.tty tty, + CASE + WHEN ingredient.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN ingredient.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + ingredient +WHERE + ingredient.tty = 'PIN' + AND ingredient.sab = 'RXNORM' diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__product_rxcuis.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__product_rxcuis.sql index 7a5ac196..b4068e3f 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__product_rxcuis.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__product_rxcuis.sql @@ -1,5 +1,16 @@ -- stg_rxnorm__product_rxcuis - -select * from {{ source('rxnorm', 'rxnorm_rxnconso') }} -where sab = 'RXNORM' - and tty in ('SCD', 'SBD', 'GPCK', 'BPCK') +SELECT + * +FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} +WHERE + sab = 'RXNORM' + AND tty IN ( + 'SCD', + 'SBD', + 'GPCK', + 'BPCK' + ) diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__products.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__products.sql index 3f57e9e4..8409dca8 100644 --- a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__products.sql +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__products.sql @@ -1,50 +1,60 @@ -- stg_rxnorm__products.sql - -select - product.rxcui as rxcui - , product.str as name - , product.tty as tty - , case - when brand_product.rxcui is not null then brand_product.clinical_product_rxcui - else product.rxcui - end as clinical_product_rxcui - , case - when product.suppress = 'N' then true - else false - end as active - , case - when product.cvf = '4096' then true - else false - end as prescribable -from {{ source('rxnorm', 'rxnorm_rxnconso') }} product -left join {{ ref('stg_rxnorm__brand_products') }} brand_product - on product.rxcui = brand_product.rxcui -where product.tty in('SCD', 'GPCK', 'SBD', 'BPCK') - and product.sab = 'RXNORM' - -/* -with - -rcp as ( - - select * from {{ ref('stg_rxnorm__clinical_products') }} - -), - -rbp as ( - - select * from {{ ref('stg_rxnorm__brand_products') }} - -) - -select distinct - coalesce(rbp.rxcui, rcp.rxcui, null) as product_rxcui - , coalesce(rbp.name, rcp.name, null) as product_name - , coalesce(rbp.tty, rcp.tty, null) as product_tty - , rcp.rxcui as clinical_product_rxcui - , rcp.name as clinical_product_name - , rcp.tty as clinical_product_tty -from rcp -left join rbp - on rbp.clinical_product_rxcui = rcp.rxcui -*/ +SELECT + product.rxcui AS rxcui, + product.str AS NAME, + product.tty AS tty, + CASE + WHEN brand_product.rxcui IS NOT NULL THEN brand_product.clinical_product_rxcui + ELSE product.rxcui + END AS clinical_product_rxcui, + CASE + WHEN product.suppress = 'N' THEN TRUE + ELSE FALSE + END AS active, + CASE + WHEN product.cvf = '4096' THEN TRUE + ELSE FALSE + END AS prescribable +FROM + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} + product + LEFT JOIN {{ ref('stg_rxnorm__brand_products') }} + brand_product + ON product.rxcui = brand_product.rxcui +WHERE + product.tty IN( + 'SCD', + 'GPCK', + 'SBD', + 'BPCK' + ) + AND product.sab = 'RXNORM' + /* + with + + rcp as ( + + select * from {{ ref('stg_rxnorm__clinical_products') }} + + ), + + rbp as ( + + select * from {{ ref('stg_rxnorm__brand_products') }} + + ) + + select distinct + coalesce(rbp.rxcui, rcp.rxcui, null) as product_rxcui + , coalesce(rbp.name, rcp.name, null) as product_name + , coalesce(rbp.tty, rcp.tty, null) as product_tty + , rcp.rxcui as clinical_product_rxcui + , rcp.name as clinical_product_name + , rcp.tty as clinical_product_tty + from rcp + left join rbp + on rbp.clinical_product_rxcui = rcp.rxcui + */ diff --git a/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__rxnrel_ttys.sql b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__rxnrel_ttys.sql new file mode 100644 index 00000000..15b29c22 --- /dev/null +++ b/dbt/sagerx/models/staging/rxnorm/stg_rxnorm__rxnrel_ttys.sql @@ -0,0 +1,46 @@ +-- stg_rxnorm__rxnrel_ttys.sql + +with + +rxnrel as ( + + select + rxcui1, + rxcui2, + rela + + from + {{ source( + 'rxnorm', + 'rxnorm_rxnrel' + ) }} + +), + +rxnconso as ( + + select + rxcui, + tty + + from + {{ source( + 'rxnorm', + 'rxnorm_rxnconso' + ) }} + + where sab = 'RXNORM' + +) + +select + rxcui1, + r1.tty as tty1, + rxcui2, + r2.tty as tty2, + rela +from rxnrel +left join rxnconso r1 + on r1.rxcui = rxcui1 +left join rxnconso r2 + on r2.rxcui = rxcui2