Datasets:
File size: 6,003 Bytes
e8c001c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | ---
id: offline-compute_MySQL_mysql_020
name: Build Volume-Lift Candidate Ad List
category: offline-compute/MySQL
timeout_seconds: 1800
modality: pure-text
engine: mysql
---
## Prompt
I need you to generate a MySQL script that builds a volume-lift candidate ad list, outputting to table `internal_platform_db.dwm_union_raise_candidate_new_ads_hf_cand_mysql_020` with partition key `p_partition='2026060905'`.
**Business Background and Objective**: The ad volume-lift system needs to jointly filter qualifying ads from multiple dimensions (creative, ad group, ecology budget, advertiser, conversion link, volume-lift configuration, creative platform) to generate a candidate list for downstream consumption. This task requires multi-table JOINs, aggregation, window function ranking, and filtering across 7 input tables, ultimately writing to the output table.
**Input Tables (full name + brief description)**:
- `internal_platform_db.dim_creative_info_f_mysql_020` (creative information)
- `internal_platform_db.dim_adgroup_info_f_mysql_020` (ad group information)
- `internal_platform_db.dim_adgroup_ecology_budget_info_di_mysql_020` (ecology budget information)
- `internal_platform_db.f_union_dim_advertiser_info_d_mysql_020` (advertiser information)
- `internal_platform_db.t_daily_conv_link_tid_dimension_mysql_020` (conversion link dimension)
- `internal_platform_db.dim_union_ad_raised_config_hf_mysql_020` (volume-lift configuration)
- `internal_platform_db.dim_tbl_creative_f_mysql_020` (creative platform information)
(Please connect to the database and query to confirm the table structures and field semantics.)
**Processing Rules**:
1. Main table a (`dim_creative_info_f`): `creative_id > 0`, aggregate by `creative_id` taking `MAX(adgroup_id)`, `MAX(advertiser_id)`, `MAX(landing_page_type)`
2. Subquery e (`dim_adgroup_info_f`) INNER JOIN:
- Filter `adgroup_id > 0`
- Filter `placement_group_id_list` containing 15 or 136 (using the `FIND_IN_SET` function)
- `begin_time >= MIN(begintime)` from the config table AND `<= MAX(endtime)`, config table conditions: `partition_time = 2026060905`, `strategyid > 0`, `20260609` within the `begintime-endtime` range (use `FROM_UNIXTIME` to convert unix timestamps to date format `yyyyMMdd` for comparison)
- Aggregate by `adgroup_id` taking `MAX(product_id)`, `MAX(optimization_goal)`, `MAX(second_optimization_goal)`, `MAX(deep_conversion_optimization_goal)`, `MAX(marketing_target_id)`, `MAX(begin_time)`, `MAX(end_time)`, `MAX(created_time)`, `MAX(exploration_strategy_id)`, `MAX(placement_group_id_list)`
3. Subquery b (`dim_adgroup_ecology_budget_info_di`) LEFT JOIN: `partition_time` between 20260607–20260608, take the latest partition's `ecology_level2_id` per `creative_id` (using the `ROW_NUMBER` window function ranked by `partition_time DESC`, taking rn=1)
4. Subquery c (`f_union_dim_advertiser_info_d`) LEFT JOIN: `partition_time` between 20260607–20260608, first aggregate by `advertiser_id + partition_time` taking `MAX(operation_industry_name) AS team`, `MAX(short_advertiser_name)`, then take the latest partition's `team`, `short_advertiser_name` per `advertiser_id` (ROW_NUMBER)
5. Subquery d (`t_daily_conv_link_tid_dimension`) LEFT JOIN: `partition_time` between 20260607–20260608, first aggregate by `tid + partition_time` taking `MAX(landingpage_link_type)`, then take the latest partition's `landingpage_link_type` per `tid` (ROW_NUMBER), join condition `a.creative_id = d.tid`
6. Subquery f (`dim_tbl_creative_f`) LEFT JOIN: `ftid > 0`, aggregate by `ftid` taking `MAX(fsmartdeliveryplatform) AS smart_delivery_platform`, join condition `a.creative_id = f.ftid`
7. Final SELECT: `partition_time = 2026060905`, `adgroup_id`, `COALESCE(advertiser_id, 0)`, `COALESCE(product_id, '')`, `CONCAT(COALESCE(optimization_goal, 0), '_', COALESCE(second_optimization_goal, 0), '_', COALESCE(deep_conversion_optimization_goal, 0)) AS mix_goal`, `COALESCE(landing_page_type, '')`, `COALESCE(marketing_target_id, 0)`, `COALESCE(ecology_level2_id, 0)`, `COALESCE(team, '')`, `COALESCE(short_advertiser_name, '')`, `COALESCE(landingpage_link_type, '')`, `COALESCE(begin_time, 0)`, `COALESCE(end_time, 0)`, `COALESCE(created_time, 0)`, `COALESCE(smart_delivery_platform, 0)`, `COALESCE(exploration_strategy_id, 0)`, `COALESCE(placement_group_id_list, '')`
**Output Requirements**:
- Target table: `internal_platform_db.dwm_union_raise_candidate_new_ads_hf_cand_mysql_020`
- Output field order: `partition_time`, `adgroup_id`, `advertiser_id`, `product_id`, `mix_goal`, `landing_page_type`, `marketing_target_id`, `ecology_level2_id`, `team`, `short_advertiser_name`, `landingpage_link_type`, `begin_time`, `end_time`, `created_time`, `smart_delivery_platform`, `exploration_strategy_id`, `placement_group_id_list`, `p_partition`
- `mix_goal` is generated by concatenating three optimization goal fields with underscores; null values are replaced with defaults using COALESCE (0 for numeric, empty string for string)
- Final deduplication by GROUP BY on all non-`partition_time` fields
- `p_partition` is fixed as `'2026060905'`
- If the target table does not exist, first create it using standard MySQL InnoDB format, then write the data
- Use standard MySQL syntax; do not use Hive/Spark SQL dialects (e.g., `INSERT OVERWRITE`, `ARRAY` type, `array_contains`, `concat_ws` for arrays, and other Hive-specific functions are not supported)
**Environment and Execution Notes**:
- Your final output must be written to the file `/tmp_workspace/result.py`, not `result.sql`
- The local MySQL is running at localhost:3306, username `root`, password `root123`
- Use Python `pymysql` in `result.py` to execute the SQL (do not use the `mysql` command-line tool)
- The script must include complete table creation (if the target table does not exist) and data writing logic
- After writing `result.py`, you must execute `python3 /tmp_workspace/result.py` yourself to verify that it runs successfully and produces correct data
|