diff --git a/scenarios/cdp_campaign_management/common/user_settings_sample.yaml b/scenarios/cdp_campaign_management/common/user_settings_sample.yaml index 846a7039..d9676c26 100644 --- a/scenarios/cdp_campaign_management/common/user_settings_sample.yaml +++ b/scenarios/cdp_campaign_management/common/user_settings_sample.yaml @@ -61,11 +61,7 @@ td: # use_distinct: false master_campaigns_tables: - # 507568: - # - - # db: taka - # table: master_campaigns_507568 - - utm_names: - utm_cv: utm_term - + 507568: + - + db: cdp_audience_507568 + table: master_campaigns diff --git a/scenarios/cdp_campaign_management/incremental_ingest.dig b/scenarios/cdp_campaign_management/incremental_ingest.dig index 1ef5a885..0750e0d8 100644 --- a/scenarios/cdp_campaign_management/incremental_ingest.dig +++ b/scenarios/cdp_campaign_management/incremental_ingest.dig @@ -35,6 +35,35 @@ _export: query: SELECT * FROM ${td.database}.${td.tables.tmp_daily_activations_info} insert_into: ${td.database}.${td.tables.daily_activations_info} ++prepare_master_campaings: + +ingest_master_campaigns: + if>: ${td.master_campaigns_tables == null || td.master_campaigns_tables[ps_id] == null} + _do: + +create_table: + td>: queries/create_master_campaigns.sql + dest_db: ${td.database} + dest_table: ${td.tables.master_campaigns} + _else_do: + +create_table: + td_ddl>: + drop_tables: + - ${td.tables.tmp_master_campaigns} + create_tables: + - ${td.tables.tmp_master_campaigns} + +ingest_to_tmp_table: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.master_campaigns_tables[ps_id]} + _do: + td>: + query: SELECT * FROM ${tbl_info.db}.${tbl_info.table} + insert_into: ${td.database}.${td.tables.tmp_master_campaigns} + + +de_duplication: + td>: queries/de_duplicate_master_campaings.sql + create_table: ${td.database}.${td.tables.master_campaigns} + + +ingest_clicks: +prepare_table: td_ddl>: @@ -43,55 +72,29 @@ _export: create_tables: - ${td.tables.tmp_clicks} - +prepare_master_campaings: - +ingest_master_campaigns: - if>: ${typeof td.master_campaigns_tables === 'undefined' || td.master_campaigns_tables == null} - _do: - +create_table: - td>: queries/create_master_campaigns.sql - dest_db: ${td.database} - dest_table: ${td.tables.master_campaigns} - _else_do: - +create_table: - td_ddl>: - drop_tables: - - ${td.tables.tmp_master_campaigns} - create_tables: - - ${td.tables.tmp_master_campaigns} - +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.master_campaigns_tables[ps_id]} - _do: - td>: - query: SELECT * FROM ${tbl_info.db}.${tbl_info.table} - insert_into: ${td.database}.${td.tables.tmp_master_campaigns} - - +de_duplication: - td>: queries/de_duplicate_master_campaings.sql - create_table: ${td.database}.${td.tables.master_campaigns} - +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.clicks_tables[ps_id]} - _do: - _export: - url_column: ${tbl_info.url_col} - input_table: ${tbl_info.table} - campaign_db: ${td.database} - filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} - master_campaigns_table: ${td.tables.master_campaigns} - - time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" - input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" - user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" - user_column_inner: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" - join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" - distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" - - td>: queries/ingest_clicks.sql - insert_into: ${td.database}.${td.tables.tmp_clicks} + if>: ${td.clicks_tables == null || td.clicks_tables[ps_id] == null} + _else_do: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.clicks_tables[ps_id]} + _do: + _export: + url_column: ${tbl_info.url_col} + input_table: ${tbl_info.table} + campaign_db: ${td.database} + filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} + master_campaigns_table: ${td.tables.master_campaigns} + + time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" + input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" + user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" + user_column_inner: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" + join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" + distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" + + td>: queries/ingest_clicks.sql + insert_into: ${td.database}.${td.tables.tmp_clicks} +write_tmp_to_dest_table: td>: @@ -118,7 +121,7 @@ _export: - ${td.tables.tmp_activations} +ingest_to_tmp_table: - if>: ${typeof td.activations_tables === 'undefined' || td.activations_tables == null || td.activations_tables[ps_id] == null || !td.activations_tables[ps_id].scan_journey_tables} + if>: ${td.activations_tables == null || td.activations_tables[ps_id] == null || !td.activations_tables[ps_id].scan_journey_tables} _do: _export: cdp_audience_db: cdp_audience_${ps_id} @@ -155,26 +158,28 @@ _export: - ${td.tables.tmp_conversions} +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.conversions_tables[ps_id]} - _do: - _export: - input_table: ${tbl_info.table} - filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} - cv_name: ${tbl_info.cv_name} - val_col: ${tbl_info.val_col} - acquired_revenue_per_person: ${tbl_info.acquired_revenue_per_person} + if>: ${td.conversions_tables == null || td.conversions_tables[ps_id] == null } + _else_do: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.conversions_tables[ps_id]} + _do: + _export: + input_table: ${tbl_info.table} + filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} + cv_name: ${tbl_info.cv_name} + val_col: ${tbl_info.val_col} + acquired_revenue_per_person: ${tbl_info.acquired_revenue_per_person} - time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" - user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" - inner_user_column: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" - input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" - join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" - distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" + time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" + user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" + inner_user_column: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" + input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" + join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" + distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" - td>: queries/ingest_conversions.sql - insert_into: ${td.database}.${td.tables.tmp_conversions} + td>: queries/ingest_conversions.sql + insert_into: ${td.database}.${td.tables.tmp_conversions} +write_tmp_to_dest_table: @@ -203,19 +208,21 @@ _export: - ${td.tables.tmp_conversion_journeys} +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.conversions_tables[ps_id]} - _do: - _export: - cv_name: ${tbl_info.cv_name} - input_db: ${td.database} - input_table_activations: ${td.tables.activations} - input_table_clicks: ${td.tables.clicks} - input_table_conversions: ${td.tables.conversions} - - td>: queries/ingest_conversion_journeys.sql - insert_into: ${td.database}.${td.tables.tmp_conversion_journeys} + if>: ${td.conversions_tables == null || td.conversions_tables[ps_id] == null } + _else_do: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.conversions_tables[ps_id]} + _do: + _export: + cv_name: ${tbl_info.cv_name} + input_db: ${td.database} + input_table_activations: ${td.tables.activations} + input_table_clicks: ${td.tables.clicks} + input_table_conversions: ${td.tables.conversions} + + td>: queries/ingest_conversion_journeys.sql + insert_into: ${td.database}.${td.tables.tmp_conversion_journeys} +write_tmp_to_dest_table: td>: diff --git a/scenarios/cdp_campaign_management/initial_ingest.dig b/scenarios/cdp_campaign_management/initial_ingest.dig index b9417b0d..a797a2a0 100644 --- a/scenarios/cdp_campaign_management/initial_ingest.dig +++ b/scenarios/cdp_campaign_management/initial_ingest.dig @@ -69,6 +69,35 @@ _export: dest_db: ${td.database} dest_table: ${td.tables.daily_activations_info} ++prepare_master_campaings: + +ingest_master_campaigns: + if>: ${td.master_campaigns_tables == null || td.master_campaigns_tables[ps_id] == null} + _do: + +create_table: + td>: queries/create_master_campaigns.sql + dest_db: ${td.database} + dest_table: ${td.tables.master_campaigns} + _else_do: + +create_table: + td_ddl>: + drop_tables: + - ${td.tables.tmp_master_campaigns} + create_tables: + - ${td.tables.tmp_master_campaigns} + +ingest_to_tmp_table: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.master_campaigns_tables[ps_id]} + _do: + td>: + query: SELECT * FROM ${tbl_info.db}.${tbl_info.table} + insert_into: ${td.database}.${td.tables.tmp_master_campaigns} + + +de_duplication: + td>: queries/de_duplicate_master_campaings.sql + create_table: ${td.database}.${td.tables.master_campaigns} + + +ingest_clicks: +prepare_table: td_ddl>: @@ -77,56 +106,29 @@ _export: create_tables: - ${td.tables.tmp_clicks} - +prepare_master_campaings: - +ingest_master_campaigns: - if>: ${typeof td.master_campaigns_tables === 'undefined' || td.master_campaigns_tables == null} - _do: - +create_table: - td>: queries/create_master_campaigns.sql - dest_db: ${td.database} - dest_table: ${td.tables.master_campaigns} - _else_do: - +create_table: - td_ddl>: - drop_tables: - - ${td.tables.tmp_master_campaigns} - create_tables: - - ${td.tables.tmp_master_campaigns} - +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.master_campaigns_tables[ps_id]} - _do: - td>: - query: SELECT * FROM ${tbl_info.db}.${tbl_info.table} - insert_into: ${td.database}.${td.tables.tmp_master_campaigns} - - +de_duplication: - td>: queries/de_duplicate_master_campaings.sql - create_table: ${td.database}.${td.tables.master_campaigns} - - +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.clicks_tables[ps_id]} - _do: - _export: - url_column: ${tbl_info.url_col} - input_table: ${tbl_info.table} - campaign_db: ${td.database} - filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} - master_campaigns_table: ${td.tables.master_campaigns} - - time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" - input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" - user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" - user_column_inner: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" - join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" - distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" - - td>: queries/ingest_clicks.sql - insert_into: ${td.database}.${td.tables.tmp_clicks} + if>: ${td.clicks_tables == null || td.clicks_tables[ps_id] == null} + _else_do: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.clicks_tables[ps_id]} + _do: + _export: + url_column: ${tbl_info.url_col} + input_table: ${tbl_info.table} + campaign_db: ${td.database} + filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} + master_campaigns_table: ${td.tables.master_campaigns} + + time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" + input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" + user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" + user_column_inner: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" + join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" + distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" + + td>: queries/ingest_clicks.sql + insert_into: ${td.database}.${td.tables.tmp_clicks} +write_tmp_to_dest_table: td>: @@ -176,7 +178,7 @@ _export: - ${td.tables.tmp_activations} +ingest_to_tmp_table: - if>: ${typeof td.activations_tables === 'undefined' || td.activations_tables == null || td.activations_tables[ps_id] == null || !td.activations_tables[ps_id].scan_journey_tables} + if>: ${td.activations_tables == null || td.activations_tables[ps_id] == null || !td.activations_tables[ps_id].scan_journey_tables} _do: _export: cdp_audience_db: cdp_audience_${ps_id} @@ -237,26 +239,28 @@ _export: - ${td.tables.tmp_conversions} +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.conversions_tables[ps_id]} - _do: - _export: - input_table: ${tbl_info.table} - filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} - cv_name: ${tbl_info.cv_name} - val_col: ${tbl_info.val_col} - acquired_revenue_per_person: ${tbl_info.acquired_revenue_per_person} + if>: ${td.conversions_tables == null || td.conversions_tables[ps_id] == null } + _else_do: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.conversions_tables[ps_id]} + _do: + _export: + input_table: ${tbl_info.table} + filter: ${(typeof tbl_info.filter === 'undefined') || tbl_info.filter} + cv_name: ${tbl_info.cv_name} + val_col: ${tbl_info.val_col} + acquired_revenue_per_person: ${tbl_info.acquired_revenue_per_person} - time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" - user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" - inner_user_column: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" - input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" - join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" - distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" + time_column: "${tbl_info.is_audience_table ? 'timestamp' : tbl_info.time_col}" + user_column: "${tbl_info.is_audience_table ? 't2.' + user_id : 't1.' + user_id}" + inner_user_column: "${tbl_info.is_audience_table ? 'cdp_customer_id' : user_id}" + input_db: "${tbl_info.is_audience_table ? 'cdp_audience_' + ps_id : tbl_info.db}" + join_part: "${tbl_info.is_audience_table ? 'JOIN cdp_audience_' + ps_id + '.customers t2 ON t1.cdp_customer_id = t2.cdp_customer_id' : ''}" + distinct: "${typeof tbl_info.use_distinct === 'undefined' || !tbl_info.use_distinct ? '' : 'DISTINCT'}" - td>: queries/ingest_conversions.sql - insert_into: ${td.database}.${td.tables.tmp_conversions} + td>: queries/ingest_conversions.sql + insert_into: ${td.database}.${td.tables.tmp_conversions} +write_tmp_to_dest_table: @@ -307,19 +311,21 @@ _export: - ${td.tables.tmp_conversion_journeys} +ingest_to_tmp_table: - _parallel: ${parallel} - for_each>: - tbl_info: ${td.conversions_tables[ps_id]} - _do: - _export: - cv_name: ${tbl_info.cv_name} - input_db: ${td.database} - input_table_activations: ${td.tables.activations} - input_table_clicks: ${td.tables.clicks} - input_table_conversions: ${td.tables.conversions} - - td>: queries/ingest_conversion_journeys.sql - insert_into: ${td.database}.${td.tables.tmp_conversion_journeys} + if>: ${td.conversions_tables == null || td.conversions_tables[ps_id] == null } + _else_do: + _parallel: ${parallel} + for_each>: + tbl_info: ${td.conversions_tables[ps_id]} + _do: + _export: + cv_name: ${tbl_info.cv_name} + input_db: ${td.database} + input_table_activations: ${td.tables.activations} + input_table_clicks: ${td.tables.clicks} + input_table_conversions: ${td.tables.conversions} + + td>: queries/ingest_conversion_journeys.sql + insert_into: ${td.database}.${td.tables.tmp_conversion_journeys} +write_tmp_to_dest_table: td>: diff --git a/scenarios/cdp_campaign_management/queries/create_conversion_journeys.sql b/scenarios/cdp_campaign_management/queries/create_conversion_journeys.sql index 97e04dde..4685dcb0 100644 --- a/scenarios/cdp_campaign_management/queries/create_conversion_journeys.sql +++ b/scenarios/cdp_campaign_management/queries/create_conversion_journeys.sql @@ -10,4 +10,10 @@ CREATE TABLE IF NOT EXISTS ${dest_db}.${dest_table} ( ,val double ,revenue double ,time_hour_from_activation double + ,utm_campaign varchar + ,utm_medium varchar + ,utm_source varchar + ,utm_content varchar + ,utm_connector varchar + ,utm_term varchar ) diff --git a/scenarios/cdp_campaign_management/queries/create_mta_conversion_journeys.sql b/scenarios/cdp_campaign_management/queries/create_mta_conversion_journeys.sql index 4b4c894a..371d0a84 100644 --- a/scenarios/cdp_campaign_management/queries/create_mta_conversion_journeys.sql +++ b/scenarios/cdp_campaign_management/queries/create_mta_conversion_journeys.sql @@ -2,6 +2,7 @@ DROP TABLE IF EXISTS ${dest_db}.${dest_table}; CREATE TABLE IF NOT EXISTS ${dest_db}.${dest_table} ( time bigint ,cv_time bigint + ,${user_id} varchar ,cv_id varchar ,position bigint ,time_hour_to_cv double diff --git a/scenarios/cdp_campaign_management/queries/ingest_conversion_journeys.sql b/scenarios/cdp_campaign_management/queries/ingest_conversion_journeys.sql index 90032e1c..46e77bcb 100644 --- a/scenarios/cdp_campaign_management/queries/ingest_conversion_journeys.sql +++ b/scenarios/cdp_campaign_management/queries/ingest_conversion_journeys.sql @@ -13,6 +13,7 @@ FROM ( ,utm_source ,utm_content ,utm_connector + ,utm_term ,0 AS cv_flg ,0 AS val ,0 AS revenue @@ -32,6 +33,7 @@ FROM ( ,utm_source ,utm_content ,utm_connector + ,utm_term ,0 AS cv_flg ,0 AS val ,0 AS revenue @@ -51,6 +53,7 @@ FROM ( ,NULL AS utm_source ,NULL AS utm_content ,NULL AS utm_connector + ,NULL AS utm_term ,1 AS cv_flg ,val ,revenue diff --git a/scenarios/cdp_campaign_management/queries/ingest_mta_conversion_journeys.sql b/scenarios/cdp_campaign_management/queries/ingest_mta_conversion_journeys.sql index 54dfd352..72c58d5f 100644 --- a/scenarios/cdp_campaign_management/queries/ingest_mta_conversion_journeys.sql +++ b/scenarios/cdp_campaign_management/queries/ingest_mta_conversion_journeys.sql @@ -33,39 +33,39 @@ WITH tbl_cv_history AS ,GREATEST(MAX(position)OVER(PARTITION BY cv_id)-3,0) AS size_middle_click FROM ( - SELECT - time - ,cv_time - ,cv_name - ,cv_id - ,ROW_NUMBER()OVER(PARTITION BY cv_id ORDER BY time, type) AS position --Ensure that 'Conversion' comes after 'Click' when time is the same. - ,time_hour_to_cv - ,CASE - WHEN type = 'Conversion' THEN 0 - WHEN time_hour_to_cv <= ${td.mta.session_model.allowable_time_to_cv} THEN 1 - ELSE 0 - END AS is_within_cv_session - ,(LEAD(time)OVER(PARTITION BY cv_id ORDER BY time) - time)/3600 AS time_hour_to_next - ,time_hour_from_activation - ,CASE - WHEN type = 'Conversion' THEN type - WHEN LEAD(type)OVER(PARTITION BY cv_id ORDER BY time, type) = 'Conversion' THEN 'Last Click' --Ensure that 'Conversion' comes after 'Click' when time is the same. - WHEN LAG(type)OVER(PARTITION BY cv_id ORDER BY time, type) IS NULL THEN 'First Click' --Ensure that 'Conversion' comes after 'Click' when time is the same. - ELSE 'Middle Click' - END AS click_type - ,type - ,${user_id} - ,activation_step_id - ,utm_source - ,utm_medium - ,utm_campaign - ,utm_content - ,utm_connector - ,cv_flg - ,val - ,revenue - ,FIRST_VALUE(revenue)OVER(PARTITION BY cv_id ORDER BY time DESC, type DESC) AS base_revenue --Ensure that 'Conversion' comes before 'Click' when time is the same. - FROM tbl_join_with_cv_history + SELECT + time + ,cv_time + ,cv_name + ,cv_id + ,ROW_NUMBER()OVER(PARTITION BY cv_id ORDER BY time, type) AS position --Ensure that 'Conversion' comes after 'Click' when time is the same. + ,time_hour_to_cv + ,CASE + WHEN type = 'Conversion' THEN 0 + WHEN time_hour_to_cv <= ${td.mta.session_model.allowable_time_to_cv} THEN 1 + ELSE 0 + END AS is_within_cv_session + ,(LEAD(time)OVER(PARTITION BY cv_id ORDER BY time) - time)/3600 AS time_hour_to_next + ,time_hour_from_activation + ,CASE + WHEN type = 'Conversion' THEN type + WHEN LEAD(type)OVER(PARTITION BY cv_id ORDER BY time, type) = 'Conversion' THEN 'Last Click' --Ensure that 'Conversion' comes after 'Click' when time is the same. + WHEN LAG(type)OVER(PARTITION BY cv_id ORDER BY time, type) IS NULL THEN 'First Click' --Ensure that 'Conversion' comes after 'Click' when time is the same. + ELSE 'Middle Click' + END AS click_type + ,type + ,${user_id} + ,activation_step_id + ,utm_source + ,utm_medium + ,utm_campaign + ,utm_content + ,utm_connector + ,cv_flg + ,val + ,revenue + ,FIRST_VALUE(revenue)OVER(PARTITION BY cv_id ORDER BY time DESC, type DESC) AS base_revenue --Ensure that 'Conversion' comes before 'Click' when time is the same. + FROM tbl_join_with_cv_history ) )