#!/bin/bash # L2 每个课时首次通关耗时统计 export PGPASSWORD='LdfjdjL83h3h3^$&**YGG*' PGHOST='bj-postgres-16pob4sg.sql.tencentcdb.com' PGPORT='28591' PGUSER='ai_member' PGDATABASE='vala_bi' PSQL="psql -h \$PGHOST -p \$PGPORT -U \$PGUSER -d \$PGDATABASE -t -A" # 获取所有 L2 章节 $PSQL -c "SELECT id FROM bi_level_unit_lesson WHERE course_level='L2' ORDER BY id;" > /tmp/l2_ch_ids.txt echo "chapter_id|season|unit|lesson|完成人数|平均用时_分钟" while read cid; do # 获取章节信息 info=$($PSQL -c "SELECT course_season||'|'||course_unit||'|'||course_lesson FROM bi_level_unit_lesson WHERE id=$cid;") # 统计首次完成: 从8张分表收集 result=$($PSQL -c " WITH fd AS ( SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_0 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_1 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_2 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_3 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_4 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_5 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_6 WHERE play_status=1 AND chapter_id=$cid UNION ALL SELECT user_id, chapter_unique_id FROM bi_user_chapter_play_record_7 WHERE play_status=1 AND chapter_id=$cid ), times AS ( SELECT fd.chapter_unique_id, COALESCE(SUM(c.interval_time),0) AS total_ms FROM fd LEFT JOIN ( SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_0 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_1 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_2 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_3 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_4 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_5 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_6 UNION ALL SELECT chapter_unique_id, interval_time FROM bi_user_component_play_record_7 ) c ON fd.chapter_unique_id = c.chapter_unique_id GROUP BY fd.chapter_unique_id ) SELECT COUNT(*), ROUND(AVG(total_ms)/60000.0, 1) FROM times WHERE total_ms > 0; " 2>&1) echo "$cid|$info|$result" done < /tmp/l2_ch_ids.txt