# Compute scores for first-level comments from view records and comment logs. Group by recall_uin + channel_id + fe (HiveSQL)

## Prompt
Task Objective: Compute the composite score for first-level comments newly added by recalled read users after their first post view, for content heat analysis.

Inputs:
- `internal_platform_db.dws_social_group_content_forum_hot_feed_recall_feed_view_hi_query_engine_122` (view records, filter `imp_hour` within [2026060811, 2026060910])
- `internal_platform_db.dwd_social_group_content_forum_hot_feed_recall_comment_log_hi_query_engine_122` (comment logs, filter `imp_hour` within [2026060811, 2026060910])
- `internal_platform_db.dwd_all_social_group_user_slice_ds_query_engine_122` (user identity, filter `imp_date >= 20260607`)

Processing Rules:
1. Build a view aggregation: From the view table, aggregate by `uin` + `channel_id` + `feed_id`, extracting the first/last view time, detail page view count, and duration.
2. Filter comments: Join the comment log with the view aggregation on `channel_id` + `feed_id`, keeping only records where the comment time is >= the first view time.
3. Enrich user identity: Left join the user table, using `member_role` as a fallback for `user_type` (use the original `user_type` when missing).
4. Compute first-level comment metrics: Group by `recall_uin` + `channel_id` + `feed_id` + `p_comment_id` (as the first-level comment ID) + `uin`, and compute:
   - Normal user like count (`action_type='comment_like'` AND `user_type=0`)
   - Author like count (`action_type='comment_like'` AND `uin=author_uin`)
   - Channel owner like count (`action_type='comment_like'` AND `user_type in (1,2)`)
   - Normal user reply count (`action_type='comment'` AND `comment_type='comment_reply'` AND `user_type=0` AND `uin<>comment_uin`)
   - Author reply count (`action_type='comment'` AND `comment_type='comment_reply'` AND `uin=author_uin` AND `uin<>comment_uin`)
   - Channel owner reply count (`action_type='comment'` AND `comment_type='comment_reply'` AND `user_type in (1,2)` AND `uin<>comment_uin`)
5. Aggregate by `recall_uin` + `channel_id` + `feed_id` + `comment_id` to compute deduplicated UV and totals:
   - UV (count distinct of users with the behavior) and counts for each like/reply type
   - Normal user average reply count (total replies / number of users who replied)
6. Compute scores (tiered rules):
   - Normal user like score: tiered by count (1→2, 2–4→4, 5–10→7, 11–20→8, 21–50→10, >50→12)
   - Author like score: tiered by UV, then +2
   - Channel owner like score: tiered by count, then +1
   - Normal user reply score: when count < 4, UV*2.5 (cap 30); when >= 4, UV*2 (cap 30)
   - Author reply score: normal user reply score + 3
   - Channel owner reply score: normal user reply score + 1
   - Average reply score: mean <=1.5→0, 1.5–3→3, >3→6
   - Total comment score: sum of the above 7 score components

Output Requirements:
- Field order: `imp_hour`, `uin` (i.e., `recall_uin`), `channel_id`, `feed_id`, `comment_id`, `comment_time`, `comment_score`, and the scores, UVs, and counts for each like/reply type (20 metric fields in total)
- `comment_time` takes the last comment time of that comment
- Partition field `imp_hour` is fixed as `2026060910`

Write Requirements:
- Output table: `internal_platform_db.dws_social_group_content_forum_hot_feed_recall_comment_score_hi_cand_query_engine_122`
- Write method: `INSERT OVERWRITE` partition `imp_hour=2026060910`

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

## Reference Solution
insert overwrite table internal_platform_db.dws_social_group_content_forum_hot_feed_recall_comment_score_hi_query_engine_122 partition (imp_hour = 2026060910)
with social_group_feed_view as
(
select
uin as recall_uin
, channel_id
, feed_id
, min(first_view_time) as first_view_time
, max(last_view_time) as last_view_time
, sum(forum_detail_cnt) as forum_detail_cnt
, sum(forum_detail_dtm_s) as forum_detail_dtm_s
from
internal_platform_db.dws_social_group_content_forum_hot_feed_recall_feed_view_hi_query_engine_122
where
imp_hour >= 2026060811 and imp_hour <= 2026060910
group by
uin
, channel_id
, feed_id
),
new_feed_comment_log as
(
select
t4.recall_uin
, t1.channel_id
, t1.feed_id
, t1.comment_id
, t1.p_comment_id
, t1.uin
, t1.comment_uin
, t1.p_comment_uin
, t1.author_uin
, t1.action_type
, t1.comment_type
, nvl(t5.member_role, t1.user_type) as user_type
, t1.time_stamp
, t4.first_view_time as first_view_time
from
(
select
channel_id
, feed_id
, comment_id
, p_comment_id
, uin
, comment_uin
, p_comment_uin
, author_uin
, action_type
, comment_type
, nvl(user_type, 0) as user_type
, time_stamp
from
internal_platform_db.dwd_social_group_content_forum_hot_feed_recall_comment_log_hi_query_engine_122
where
imp_hour >= 2026060811 and imp_hour <= 2026060910
) t1
join
(
select
recall_uin
, channel_id
, feed_id
, first_view_time
from
social_group_feed_view
) t4
on
t1.channel_id = t4.channel_id
and t1.feed_id = t4.feed_id
left join
(
select
uin
, channel_id
, member_role
from
internal_platform_db.dwd_all_social_group_user_slice_ds_query_engine_122
where
imp_date >= 20260607
) t5
on
t1.uin = t5.uin
and t1.channel_id = t5.channel_id
where
t1.time_stamp >= t4.first_view_time
)
select
recall_uin as uin
, channel_id
, feed_id
, comment_id
, comment_time
, normal_comment_like_score + key_author_comment_like_score + key_owner_comment_like_score
+ normal_comment_reply_score + key_author_comment_reply_score + key_owner_comment_reply_score
+ avg_normal_comment_reply_score as comment_score
, normal_comment_like_score
, key_author_comment_like_score
, key_owner_comment_like_score
, normal_comment_reply_score
, key_author_comment_reply_score
, key_owner_comment_reply_score
, avg_normal_comment_reply_score
, normal_comment_like_uv
, normal_comment_like_cnt
, key_author_comment_like_uv
, key_author_comment_like_cnt
, key_owner_comment_like_uv
, key_owner_comment_like_cnt
, normal_comment_reply_uv
, normal_comment_reply_cnt
, avg_normal_comment_reply_cnt
, key_author_comment_reply_uv
, key_author_comment_reply_cnt
, key_owner_comment_reply_uv
, key_owner_comment_reply_cnt
from
(
select
recall_uin
, channel_id
, feed_id
, comment_id
, last_comment_time as comment_time
, case
when normal_comment_like_cnt = 1 then 2
when normal_comment_like_cnt > 1 and normal_comment_like_cnt < 5 then 4
when normal_comment_like_cnt >= 5 and normal_comment_like_cnt <= 10 then 7
when normal_comment_like_cnt > 10 and normal_comment_like_cnt <= 20 then 8
when normal_comment_like_cnt > 20 and normal_comment_like_cnt <= 50 then 10
when normal_comment_like_cnt > 50 then 12
else 0
end as normal_comment_like_score
, case
when key_author_comment_like_uv = 1 then 2+2
when key_author_comment_like_uv > 1 and key_author_comment_like_uv < 5 then 4+2
when key_author_comment_like_uv >= 5 and key_author_comment_like_uv <= 10 then 7+2
when key_author_comment_like_uv > 10 and key_author_comment_like_uv <= 20 then 8+2
when key_author_comment_like_uv > 20 and key_author_comment_like_uv <= 50 then 10+2
when key_author_comment_like_uv > 50 then 12+2
else 0
end as key_author_comment_like_score
, case
when key_owner_comment_like_cnt = 1 then 2+1
when key_owner_comment_like_cnt > 1 and key_owner_comment_like_cnt < 5 then 4+1
when key_owner_comment_like_cnt >= 5 and key_owner_comment_like_cnt <= 10 then 7+1
when key_owner_comment_like_cnt > 10 and key_owner_comment_like_cnt <= 20 then 8+1
when key_owner_comment_like_cnt > 20 and key_owner_comment_like_cnt <= 50 then 10+1
when key_owner_comment_like_cnt > 50 then 12+1
else 0
end as key_owner_comment_like_score
, case
when normal_comment_reply_cnt >= 1 and normal_comment_reply_cnt < 4 then
case when normal_comment_reply_uv * 2.5 > 30 then 30 else normal_comment_reply_uv * 2.5 end
when normal_comment_reply_cnt >= 4 then
case when normal_comment_reply_uv * 2 > 30 then 30 else normal_comment_reply_uv * 2 end
else 0
end as normal_comment_reply_score
, case
when key_author_comment_reply_cnt >= 1 and key_author_comment_reply_cnt < 4 then
case when key_author_comment_reply_uv * 2.5 + 3 > 30 then 30 else key_author_comment_reply_uv * 2.5 + 3 end
when key_author_comment_reply_cnt >= 4 then
case when key_author_comment_reply_uv * 2 + 3 > 30 then 30 else key_author_comment_reply_uv * 2 + 3 end
else 0
end as key_author_comment_reply_score
, case
when key_owner_comment_reply_cnt >= 1 and key_owner_comment_reply_cnt < 4 then
case when key_owner_comment_reply_uv * 2.5 + 1 > 30 then 30 else key_owner_comment_reply_uv * 2.5 + 1 end
when key_owner_comment_reply_cnt >= 4 then
case when key_owner_comment_reply_uv * 2 + 1 > 30 then 30 else key_owner_comment_reply_uv * 2 + 1 end
else 0
end as key_owner_comment_reply_score
, case
when avg_normal_comment_reply_cnt <= 1.5 then 0
when avg_normal_comment_reply_cnt > 1.5 and avg_normal_comment_reply_cnt <= 3 then 3
when avg_normal_comment_reply_cnt > 3 then 6
else 0
end as avg_normal_comment_reply_score
, normal_comment_like_uv
, normal_comment_like_cnt
, key_author_comment_like_uv
, key_author_comment_like_cnt
, key_owner_comment_like_uv
, key_owner_comment_like_cnt
, normal_comment_reply_uv
, normal_comment_reply_cnt
, avg_normal_comment_reply_cnt
, key_author_comment_reply_uv
, key_author_comment_reply_cnt
, key_owner_comment_reply_uv
, key_owner_comment_reply_cnt
from
(
select
recall_uin
, channel_id
, feed_id
, comment_id
, max(last_comment_time) as last_comment_time
, count(distinct case when normal_comment_like_cnt > 0 then uin else null end) as normal_comment_like_uv
, sum(normal_comment_like_cnt) as normal_comment_like_cnt
, count(distinct case when key_author_comment_like_cnt > 0 then uin else null end) as key_author_comment_like_uv
, sum(key_author_comment_like_cnt) as key_author_comment_like_cnt
, count(distinct case when key_owner_comment_like_cnt > 0 then uin else null end) as key_owner_comment_like_uv
, sum(key_owner_comment_like_cnt) as key_owner_comment_like_cnt
, count(distinct case when normal_comment_reply_cnt > 0 then uin else null end) as normal_comment_reply_uv
, sum(normal_comment_reply_cnt) as normal_comment_reply_cnt
, sum(normal_comment_reply_cnt)/count(distinct case when normal_comment_reply_cnt > 0 then uin else null end) as avg_normal_comment_reply_cnt
, count(distinct case when key_author_comment_reply_cnt > 0 then uin else null end) as key_author_comment_reply_uv
, sum(key_author_comment_reply_cnt) as key_author_comment_reply_cnt
, count(distinct case when key_owner_comment_reply_cnt > 0 then uin else null end) as key_owner_comment_reply_uv
, sum(key_owner_comment_reply_cnt) as key_owner_comment_reply_cnt
from
(
select
recall_uin
, channel_id
, feed_id
, p_comment_id as comment_id
, max(time_stamp) as last_comment_time
, uin
, sum(case when action_type = 'comment_like' and user_type in (0) then 1 else 0 end) as normal_comment_like_cnt
, sum(case when action_type = 'comment_like' and uin = author_uin then 1 else 0 end) as key_author_comment_like_cnt
, sum(case when action_type = 'comment_like' and user_type in (1,2)  then 1 else 0 end) as key_owner_comment_like_cnt
, sum(case when action_type = 'comment' and comment_type = 'comment_reply' and user_type in (0) and uin <> comment_uin then 1 else 0 end) normal_comment_reply_cnt
, sum(case when action_type = 'comment' and comment_type = 'comment_reply' and uin = author_uin and uin <> comment_uin then 1 else 0 end) key_author_comment_reply_cnt
, sum(case when action_type = 'comment' and comment_type = 'comment_reply' and user_type in (1,2) and uin <> comment_uin then 1 else 0 end) key_owner_comment_reply_cnt
from
new_feed_comment_log
where
action_type = 'comment_like' or (action_type = 'comment' and comment_type = 'comment_reply')
group by
recall_uin
, channel_id
, feed_id
, p_comment_id
, uin
) t
group by
recall_uin
, channel_id
, feed_id
, comment_id
) tt
) t