# Hot Table Governance Metadata Wide Table Aggregation (PrestoSQL)

## Prompt
**Task Objective**: From the hot table governance metadata detail table, filter valid hot table records (heat > 0, excluding temporary/test tables), aggregate by database name and table name to take the maximum of each metric, and write the results to the output table.

**Input Table**:
- `internal_platform_db.ads_gov_cost_table_govern_detail_df_prestosql_005` (hot table governance metadata detail table)
  - `imp_date` BIGINT — date partition
  - `db_name` STRING — database name
  - `table_name` STRING — table name
  - `heat` BIGINT — heat
  - `owner` STRING — owner
  - `storage_detail` DOUBLE — storage detail
  - `task_cnt` BIGINT — task count
  - `table_type` STRING — table type
  - `govern_status` STRING — governance status
  - `last_access_time` STRING — last access time
  - `table_comment` STRING — table comment
  - `partition_cnt` BIGINT — partition count
  - `file_format` STRING — file format
  - `table_size` DOUBLE — table size
  - `avg_file_size` DOUBLE — average file size
  - `is_partitioned` INT — whether partitioned
  - `lifecycle_days` INT — lifecycle days
  - `create_time` STRING — creation time
  - `modify_time` STRING — modification time
  - `project_name` STRING — project name

**Filter Conditions**:
- `imp_date = 20260507`
- `heat > 0`
- `table_name NOT LIKE '%temp%'`
- `db_name NOT LIKE '%test%'`

**Output Requirements**:
- Target table: `internal_platform_db.dwd_hot_metadata_table_cand_prestosql_005`
- Output fields and order: `dt` STRING, `db_name` STRING, `table_name` STRING, `owner` STRING, `storage_detail` DOUBLE, `heat` BIGINT, `task_cnt` BIGINT, `table_type` STRING, `govern_status` STRING, `last_access_time` STRING, `table_comment` STRING, `partition_cnt` BIGINT, `file_format` STRING, `table_size` DOUBLE, `avg_file_size` DOUBLE, `is_partitioned` INT, `lifecycle_days` INT, `create_time` STRING, `modify_time` STRING, `project_name` STRING
- `dt`: Fixed value `'20260507'`
- Group by `db_name`, `table_name`, apply `MAX()` to all other fields
- 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.dwd_hot_metadata_table_prestosql_005
SELECT
  '20260507' AS dt,
  db_name,
  table_name,
  MAX(owner) AS owner,
  MAX(storage_detail) AS storage_detail,
  MAX(heat) AS heat,
  MAX(task_cnt) AS task_cnt,
  MAX(table_type) AS table_type,
  MAX(govern_status) AS govern_status,
  MAX(last_access_time) AS last_access_time,
  MAX(table_comment) AS table_comment,
  MAX(partition_cnt) AS partition_cnt,
  MAX(file_format) AS file_format,
  MAX(table_size) AS table_size,
  MAX(avg_file_size) AS avg_file_size,
  MAX(is_partitioned) AS is_partitioned,
  MAX(lifecycle_days) AS lifecycle_days,
  MAX(create_time) AS create_time,
  MAX(modify_time) AS modify_time,
  MAX(project_name) AS project_name
FROM internal_platform_db.ads_gov_cost_table_govern_detail_df_prestosql_005
WHERE imp_date = 20260507
  AND heat > 0
  AND table_name NOT LIKE '%temp%'
  AND db_name NOT LIKE '%test%'
GROUP BY db_name, table_name