# Aggregate Notebook instance runtime statistics by trace_id and p_date for the day: Input table w (HiveSQL)

## Prompt
Task Objective: Aggregate Notebook instance runtime statistics grouped by `trace_id` and `p_date`.

Input: `internal_platform_db.dws_notebook_instance_execute_minute_stat_d_query_engine_019`
- Partition field: `dt` (STRING, format YYYYMMDD)
- Key fields: `trace_id`, `p_date`, `datawd_project_id`, `datawd_task_id`, `datawd_task_instance_id`, `compute_type`, `status_code`, `instance_run_time`, `code_run_time`, `resource_wait_time`, `code_start_time`, `code_end_time`, `instance_start_time`, `instance_end_time`, `serving_id`, `is_permanent`, `apply_for_gpu_count`, `code_gpu_count` (numeric), `used_gpu_count` (numeric), `is_code_running` (INT), `code_used_gpu_count` (numeric)

Processing Rules:
- Filter condition: `dt = '20260507'`
- No joins, single-table processing
- Aggregation: Group by `trace_id`, `p_date`
  - Most fields use `MAX`: `datawd_project_id`, `datawd_task_id`, `datawd_task_instance_id`, `compute_type`, `status_code`, `instance_run_time`, `code_run_time`, `resource_wait_time`, `code_start_time`, `code_end_time`, `instance_start_time`, `instance_end_time`, `serving_id`, `is_permanent`, `apply_for_gpu_count`
  - `code_gpu_count`: Use `AVG`
  - `instance_gpu_util`: `AVG(used_gpu_count / code_gpu_count) * 100`
  - `code_gpu_util`: `AVG(IF(is_code_running = 1, code_used_gpu_count / code_gpu_count, NULL)) * 100`

Output Requirements:
- Output field order: `dt` (STRING), `trace_id` (STRING), `p_date` (STRING), `datawd_project_id` (STRING), `datawd_task_id` (STRING), `datawd_task_instance_id` (STRING), `compute_type` (STRING), `status_code` (INT), `instance_run_time` (INT), `code_run_time` (INT), `resource_wait_time` (INT), `code_start_time` (STRING), `code_end_time` (STRING), `instance_start_time` (STRING), `instance_end_time` (STRING), `serving_id` (STRING), `is_permanent` (BOOLEAN), `apply_for_gpu_count` (INT), `code_gpu_count` (INT), `instance_gpu_util` (DOUBLE), `code_gpu_util` (DOUBLE)
- Partition field: `dt` (STRING, format YYYYMMDD)

Write Requirements:
- Target table: `internal_platform_db.dws_notebook_instance_execute_stat_d_cand_query_engine_019`
- Write method: `INSERT OVERWRITE` partitioned by `dt`
- If the target table does not exist, first create the table using standard Hive ORC storage, then write the data

Please write the final HiveSQL to `result.sql` and execute it.

## Reference Solution
INSERT overwrite TABLE internal_platform_db.dws_notebook_instance_execute_stat_d_query_engine_019 PARTITION (dt = '20260507')
SELECT
  t1.trace_id,
  t1.p_date,
  t1.datawd_project_id,
  t1.datawd_task_id,
  t1.datawd_task_instance_id,
  t1.compute_type,
  t1.status_code,
  t1.instance_run_time,
  t1.code_run_time,
  t1.resource_wait_time,
  t1.code_start_time,
  t1.code_end_time,
  t1.instance_start_time,
  t1.instance_end_time,
  t1.serving_id,
  t1.is_permanent,
  t1.apply_for_gpu_count,
  t1.code_gpu_count,
  t1.instance_gpu_util,
  t1.code_gpu_util
FROM (
  SELECT
    trace_id,
    p_date,
    MAX(datawd_project_id) as datawd_project_id,
    MAX(datawd_task_id) as datawd_task_id,
    MAX(datawd_task_instance_id) as datawd_task_instance_id,
    MAX(compute_type) as compute_type,
    MAX(status_code) as status_code,
    MAX(instance_run_time) as instance_run_time,
    MAX(code_run_time) as code_run_time,
    MAX(resource_wait_time) as resource_wait_time,
    MAX(code_start_time) as code_start_time,
    MAX(code_end_time) as code_end_time,
    MAX(instance_start_time) as instance_start_time,
    MAX(instance_end_time) as instance_end_time,
    MAX(serving_id) as serving_id,
    MAX(is_permanent) as is_permanent,
    MAX(apply_for_gpu_count) as apply_for_gpu_count,
    AVG(code_gpu_count) as code_gpu_count,
    AVG(used_gpu_count / code_gpu_count) * 100  as instance_gpu_util,
    AVG(if(is_code_running = 1, code_used_gpu_count / code_gpu_count, null)) * 100 as code_gpu_util
  FROM internal_platform_db.dws_notebook_instance_execute_minute_stat_d_query_engine_019
  WHERE dt = '20260507'
  GROUP BY trace_id, p_date
) t1
;