# Ad ASA Attribution Migration (PrestoSQL)

## Prompt
**Task Objective**: From the ad attribution raw logs, filter valid records of two attribution types (ad_attribution and huawei), aggregate the log count and deduplicated user count by version and media dimensions, and write the results to the output table.

**Input Table**:
- `internal_platform_db.t_sh_event_log_v2_06000061157_prestosql_002` (ad attribution raw logs)
  - `ftime` STRING — time
  - `extinfo` STRING — extended information
  - `ad_version` STRING — ad version (ad_attribution/huawei)
  - `guid` STRING — user identifier
  - `ch` STRING — channel
  - `first_date` STRING — first date
  - `last_date` STRING — last date
  - `click_date` STRING — click date
  - `attribution` STRING — attribution flag
  - `conversion_date` STRING — conversion date
  - `conversion_type` STRING — conversion type
  - `creativeset_id` STRING — creative set ID
  - `org_id` STRING — organization ID
  - `campaign_id` STRING — campaign ID
  - `adgroup_id` STRING — ad group ID
  - `keyword_id` STRING — keyword ID
  - `country_oregion` STRING — country/region
  - `enter_ag_time` STRING — ad group entry time
  - `installed_finish_time` STRING — installation completion time
  - `start_download_time` STRING — download start time
  - `sub_channel` STRING — sub-channel
  - `callback` STRING — callback information
  - `task_id` STRING — task ID
  - `sub_task_id` STRING — sub-task ID
  - `rta_id` STRING — RTA identifier
  - `device_id` STRING — device_id identifier
  - `ext_info1` STRING — extended information 1
  - `ext_info2` STRING — extended information 2
  - `caid` STRING — CAID
  - `claim_type` STRING — claim type
  - `ai_assistant_device_id` STRING — YB device_id
  - `ds` STRING — partition field

**Filter Conditions**:
- `ds = '2026060919'`
- Valid record condition: `(ad_version = 'ad_attribution' AND attribution = 'true') OR (ad_version = 'phonebrandh' AND callback != '')`

**Output Requirements**:
- Target table: `internal_platform_db.t_my_ads_tracking_log_metrics_cand_prestosql_002`
- Output fields and order: `ds` BIGINT, `ad_version` STRING, `media_id` INT, `log_num` BIGINT, `log_num_qimei` BIGINT
- `ds`: Cast the original table's `ds` field to BIGINT
- `media_id`: `CASE ad_version WHEN 'ad_attribution' THEN 39 WHEN 'phonebrandh' THEN 28 END`
- `log_num`: `COUNT(*)` log count
- `log_num_qimei`: `COUNT(DISTINCT device_id)` deduplicated user count
- Group by `ds`, `ad_version`
- If the target table does not exist, first create the table, then write the data
- Use Presto/Trino SQL syntax; do not use Hive/Spark SQL dialects

**Environment and Execution Notes**:
- Presto is running, connected via the Hive catalog
- Execute SQL: `presto-cli --catalog hive --schema internal_platform_db -f /tmp_workspace/result.sql`
- After writing `result.sql`, you must execute it yourself to verify that it runs successfully and produces correct data

## Reference Solution
INSERT OVERWRITE TABLE internal_platform_db.t_my_ads_tracking_log_metrics_prestosql_002
SELECT
  CAST(ds AS BIGINT) AS ds,
  ad_version,
  CASE ad_version WHEN 'ad_attribution' THEN 39 WHEN 'phonebrandh' THEN 28 END AS media_id,
  COUNT(*) AS log_num,
  COUNT(DISTINCT device_id) AS log_num_qimei
FROM internal_platform_db.t_sh_event_log_v2_06000061157_prestosql_002
WHERE ds = '2026060919'
  AND ((ad_version = 'ad_attribution' AND attribution = 'true')
    OR (ad_version = 'phonebrandh' AND callback != ''))
GROUP BY ds, ad_version