dicemy's picture
Upload 655 files
e8c001c verified
|
Raw
History Blame Contribute Delete
4.26 kB
metadata
id: offline-compute_MySQL_mysql_018
name: URL Safety Detection Multi-Dimensional Access Statistics Report
category: offline-compute/MySQL
timeout_seconds: 1800
modality: pure-text
engine: mysql

Prompt

I need you to generate a MySQL script that computes multi-dimensional access statistics from the URL safety detection detail table, producing a URL access statistics report.

Business Background and Objective: The URL safety detection system collects URL access records for each taid daily. It needs to compute UV and its 1d/7d/30d/1m periodic metrics across multiple dimensions (taid, taid+url, taid+domain/site, taid+cgi), and output a single-row summary result to the report table.

Input Tables (full name + brief description):

  • internal_platform_db.t_dws_urlsafe_rela_mobile_browser_taid_url_di_mysql_018 (URL safety detection detail table)
  • Historical partition data: Historical records in the output table itself where ds = 20260531, channel = 'mobile_browser' (left join source, used to obtain 1m metrics)

(Please connect to the database and query to confirm the table structures and field semantics.)

Filter Condition:

  • Input table: ds > 20260509 AND ds <= 20260608

Processing Rules:

Subquery a (taid-level UV statistics)

  • Group by taid, compute:
    • uv_1d = COUNT(IF(ds_max = 20260608, 1, NULL))
    • uv_avg_7d = (COUNT(state_6d=1) + ... + COUNT(state_daily=1)) / 7, where state_*d = MAX(IF(ds = corresponding_date, 1, 0))
    • uv_7d = COUNT(IF(ds_max > 20260601, 1, NULL))
    • uv_30d = COUNT(1)

Subquery c (taid+url-level statistics)

  • Group by taid, url and aggregate, computing userid_url_1d/avg_7d/7d/30d using the same logic as above

Subquery d (taid+domain/site-level statistics)

  • First group by taid, site and aggregate domain, ds_max, state_*d
    • Domain value rule: Under the same taid+site, if multiple domains exist, take MAX(domain) (lexicographically largest value) as the domain attribution for that site
  • Then group by taid, domain and aggregate, computing:
    • userid_domain_1d/avg_7d/7d/30d
    • userid_site_1d = SUM(site_cnt_daily)
    • userid_site_avg_7d = (SUM(site_cnt_6d) + ... + SUM(site_cnt_daily)) / 7
    • userid_site_7d = SUM(site_cnt_7d)
    • userid_site_30d = SUM(site_cnt)

Subquery e (taid+cgi-level statistics)

  • Group by taid, cgi and aggregate, computing userid_cgi_1d/avg_7d/7d/30d using the same logic as above

Join Logic

  • a LEFT JOIN b (historical partition, joined on data_par): IFNULL(b.uv_1m, 0) and 4 other fields
  • a JOIN c, d, e (all inner joins, joined on data_par)

Final Output Fields (in order)

  • access_type = '网址检测API'
  • access_channel = '手机IM平台Q浏览器'
  • uv_1d, uv_avg_7d (ROUND(x, 0)), uv_7d, uv_30d, uv_1m
  • userid_url_1d, userid_url_avg_7d (ROUND), userid_url_7d, userid_url_30d, userid_url_1m
  • userid_domain_1d, userid_domain_avg_7d (ROUND), userid_domain_7d, userid_domain_30d, userid_domain_1m
  • userid_site_1d, userid_site_avg_7d (ROUND), userid_site_7d, userid_site_30d, userid_site_1m
  • userid_cgi_1d, userid_cgi_avg_7d (ROUND), userid_cgi_7d, userid_cgi_30d, userid_cgi_1m

Output Requirements:

  • Target table: internal_platform_db.t_app_urlsafe_report_url_access_stat_di_cand_mysql_018
  • Single-row result, 27 fields
  • All avg fields retain 0 decimal places (using ROUND)
  • 1m fields use IFNULL to handle NULL as 0
  • 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

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