Advertisement
apoorv_me

Untitled

Apr 3rd, 2025 (edited)
412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.16 KB | None | 0 0
  1. WITH max_date_cte AS (
  2.     SELECT MAX(year_month) AS max_date_month FROM {val_fact_weekly}
  3. ),
  4.  
  5. val_data_cte AS (
  6.     SELECT
  7.         claim_id, patient_sk, plan_sk, product_sk, physician_sk, fill_date,
  8.         mastered_claim_status_code AS claim_status_code, new_to_product_flag AS val_nbrx,
  9.         YEAR(fill_date) AS mpd_year, pharmacy_distribution_desc AS channel,
  10.         source_initial_final_flag AS claim_status, source_flag, source_market, market_code
  11.     FROM {val_fact_weekly}
  12.     WHERE source_market IN ('MIGRAINE', 'PSYCH')
  13.         AND source_flag = '{source_flag}'
  14.         AND UPPER(market_code) LIKE ANY ('ACUTE', 'MIGRAINE', 'PREVENTIVE', 'PSYCH')
  15.         AND UPPER(market_code) NOT LIKE '%CON%'
  16.         AND fill_date >= (
  17.             SELECT DISTINCT ADD_MONTHS(TO_DATE(CONCAT(max_date_month,'01'), 'yyyyMMdd'), -16)
  18.             FROM max_date_cte
  19.         )
  20. ),
  21.  
  22. val_product_cte AS (
  23.     SELECT DISTINCT
  24.         product_sk, source_product_ndc_code AS ndc11, mdm_product_id AS child_product_id,
  25.         source_product_name AS product_name, abbvie_product_id
  26.     FROM {val_dim_prd_weekly}
  27.     WHERE source_market IN ('MIGRAINE', 'PSYCH')
  28.         AND source_flag = '{source_flag}'
  29.         AND source_product_name IN ('VRAYLAR', 'QULIPTA', 'UBRELVY')
  30. ),
  31.  
  32. val_product_data_cte AS (
  33.     SELECT * FROM val_data_cte
  34.     INNER JOIN val_product_cte USING (product_sk)
  35.     WHERE child_product_id IS NOT NULL
  36. ),
  37.  
  38. val_plan_cte AS (
  39.     SELECT DISTINCT
  40.         plan_sk, source_plan_id AS primary_plan_id, source_payer_name AS primary_payor_name,
  41.         abbvie_plan_name, abbvie_plan_id, abbvie_plan_channel, pfv_payer_name,
  42.         pfv_payer_channel, source_plan_channel, source_plan_name, pfv_plan_id
  43.     FROM {val_dim_plan_weekly}
  44.     WHERE source_market IN ('MIGRAINE', 'PSYCH')
  45.         AND source_flag = '{source_flag}'
  46. ),
  47.  
  48. val_plan_data_cte AS (
  49.     SELECT * FROM val_product_data_cte
  50.     LEFT JOIN val_plan_cte USING (plan_sk)
  51. ),
  52.  
  53. plan_bridge_cte AS (
  54.     SELECT DISTINCT PAYER_PLAN_IDENTIFIER AS payer_plan_id, payer_entity_identifier AS pfv_plan_id
  55.     FROM pfv_ma360_plan_formulary.pfv_hierarchy_ims_bridge_tbl
  56. ),
  57.  
  58. val_plan_data_final_cte AS (
  59.     SELECT * FROM val_plan_data_cte
  60.     LEFT JOIN plan_bridge_cte USING (pfv_plan_id)
  61. ),
  62.  
  63. acc_map_cte AS (
  64.     SELECT
  65.         account_id_level1 AS abbvie_plan_id, channel_sk, account_name_level1,
  66.         account_name_level2, account_name_level3, account_name_level4
  67.     FROM {acc_map_tbl}
  68.     WHERE account_type_level = 'PLAN'
  69. ),
  70.  
  71. val_acc_map_join_cte AS (
  72.     SELECT * FROM val_plan_data_final_cte
  73.     INNER JOIN acc_map_cte USING (abbvie_plan_id)
  74. ),
  75.  
  76. data_with_mdm_cte AS (
  77.     SELECT DISTINCT child_product_id, product_group_id, market_id, product_group_name
  78.     FROM {mdm_product_hierarchy_wide_tbl}
  79.     WHERE market_id IN ('MKT02416165661','MKT33405150037', 'MKT33314581420')
  80.         AND market_name LIKE '%MA_ARC'
  81.         AND product_group_id != market_id
  82.         AND hierarchy_level IN ('PRD', 'PKGSZ')
  83.         AND dds_active_flag = 'Y'
  84.         AND future_current_flag = 'C'
  85. )
  86.  
  87. SELECT *
  88. FROM val_acc_map_join_cte
  89. INNER JOIN data_with_mdm_cte USING (child_product_id);
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement