# News Plugin Send Process Multi-Dimensional Aggregation (PrestoSQL)

## Prompt
**Task Objective**: From the news plugin send process flow table, aggregate and compute metrics such as send count, send UV, panel exposure count, panel exposure UV, inner-page PV, and inner-page UV by the scene (`pos_desc`) dimension, across two time windows — current day and last 1 hour — and write the results to the output table.

**Input Table**:
- `internal_platform_db.dwm_news_plugin_ai_assistant_send_process_flow_hi_prestosql_006` (news plugin send process flow table)
  - `imp_hour` BIGINT — hourly partition
  - `user_id` STRING — user ID
  - `content_id` STRING — content ID
  - `push_id` STRING — push ID
  - `pos` STRING — position
  - `send_cnt` BIGINT — send count
  - `panel_exp_cnt` BIGINT — panel exposure count
  - `pgin_pv` BIGINT — inner-page PV
  - `pos_desc` STRING — position description (scene)
  - `account_code` STRING — business code

**Output Requirements**:
- Target table: `internal_platform_db.ads_news_plugin_ai_assistant_hourly_report_cand_prestosql_006`
- Output fields and order:
  - `imp_hour` BIGINT — hourly partition
  - `scene` STRING — scene (`pos_desc`)
  - `send_cnt_dth` BIGINT — current day send count (`SUM(send_cnt) WHERE imp_hour <= current hour`)
  - `send_uv_dth` BIGINT — current day send UV (`COUNT(DISTINCT user_id) WHERE imp_hour <= current hour`)
  - `panel_exp_cnt_dth` BIGINT — current day panel exposure count (`SUM(panel_exp_cnt) WHERE imp_hour <= current hour`)
  - `panel_exp_uv_dth` BIGINT — current day panel exposure UV (`COUNT(DISTINCT user_id) WHERE panel_exp_cnt > 0 AND imp_hour <= current hour`)
  - `pgin_pv_dth` BIGINT — current day inner-page PV (`SUM(pgin_pv) WHERE imp_hour <= current hour`)
  - `pgin_uv_dth` BIGINT — current day inner-page UV (`COUNT(DISTINCT user_id) WHERE pgin_pv > 0 AND imp_hour <= current hour`)
  - `send_cnt_1h` BIGINT — last 1 hour send count (`SUM(send_cnt) WHERE imp_hour = current hour`)
  - `send_uv_1h` BIGINT — last 1 hour send UV
  - `panel_exp_cnt_1h` BIGINT — last 1 hour panel exposure count
  - `panel_exp_uv_1h` BIGINT — last 1 hour panel exposure UV
  - `pgin_pv_1h` BIGINT — last 1 hour inner-page PV
  - `pgin_uv_1h` BIGINT — last 1 hour inner-page UV

- Simplified explanation: For each `imp_hour` + `pos_desc` combination, compute the current day cumulative (dth) and current hour (1h) aggregation metrics
- 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.ads_news_plugin_ai_assistant_hourly_report_prestosql_006
SELECT
  h.imp_hour,
  h.pos_desc AS scene,
  h.send_cnt_dth,
  h.send_uv_dth,
  h.panel_exp_cnt_dth,
  h.panel_exp_uv_dth,
  h.pgin_pv_dth,
  h.pgin_uv_dth,
  h.send_cnt_1h,
  h.send_uv_1h,
  h.panel_exp_cnt_1h,
  h.panel_exp_uv_1h,
  h.pgin_pv_1h,
  h.pgin_uv_1h
FROM (
  SELECT
    t1.imp_hour,
    t1.pos_desc,
    t1.send_cnt AS send_cnt_dth,
    t1.send_uv AS send_uv_dth,
    t1.panel_exp_cnt AS panel_exp_cnt_dth,
    t1.panel_exp_uv AS panel_exp_uv_dth,
    t1.pgin_pv AS pgin_pv_dth,
    t1.pgin_uv AS pgin_uv_dth,
    t2.send_cnt AS send_cnt_1h,
    t2.send_uv AS send_uv_1h,
    t2.panel_exp_cnt AS panel_exp_cnt_1h,
    t2.panel_exp_uv AS panel_exp_uv_1h,
    t2.pgin_pv AS pgin_pv_1h,
    t2.pgin_uv AS pgin_uv_1h
  FROM (
    SELECT
      a.imp_hour,
      a.pos_desc,
      SUM(b.send_cnt) AS send_cnt,
      COUNT(DISTINCT b.user_id) AS send_uv,
      SUM(b.panel_exp_cnt) AS panel_exp_cnt,
      COUNT(DISTINCT CASE WHEN b.panel_exp_cnt > 0 THEN b.user_id END) AS panel_exp_uv,
      SUM(b.pgin_pv) AS pgin_pv,
      COUNT(DISTINCT CASE WHEN b.pgin_pv > 0 THEN b.user_id END) AS pgin_uv
    FROM (SELECT DISTINCT imp_hour, pos_desc FROM internal_platform_db.dwm_news_plugin_ai_assistant_send_process_flow_hi_prestosql_006) a
    JOIN internal_platform_db.dwm_news_plugin_ai_assistant_send_process_flow_hi_prestosql_006 b
      ON a.pos_desc = b.pos_desc AND b.imp_hour <= a.imp_hour
    GROUP BY a.imp_hour, a.pos_desc
  ) t1
  JOIN (
    SELECT
      imp_hour,
      pos_desc,
      SUM(send_cnt) AS send_cnt,
      COUNT(DISTINCT user_id) AS send_uv,
      SUM(panel_exp_cnt) AS panel_exp_cnt,
      COUNT(DISTINCT CASE WHEN panel_exp_cnt > 0 THEN user_id END) AS panel_exp_uv,
      SUM(pgin_pv) AS pgin_pv,
      COUNT(DISTINCT CASE WHEN pgin_pv > 0 THEN user_id END) AS pgin_uv
    FROM internal_platform_db.dwm_news_plugin_ai_assistant_send_process_flow_hi_prestosql_006
    GROUP BY imp_hour, pos_desc
  ) t2 ON t1.imp_hour = t2.imp_hour AND t1.pos_desc = t2.pos_desc
) h