diff --git a/app/services/management/school_data_grow_service.rb b/app/services/management/school_data_grow_service.rb index 18b72751..2baf8087 100644 --- a/app/services/management/school_data_grow_service.rb +++ b/app/services/management/school_data_grow_service.rb @@ -43,8 +43,12 @@ class Management::SchoolDataGrowService def grow_summary @_grow_summary ||= begin - reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id AND #{date_condition_sql}") - .joins('LEFT JOIN school_daily_active_users au ON au.school_daily_report_id = sdr.id') + reports = School.joins("LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id") + .where(date_condition_sql) + + subquery = SchoolDailyActiveUser.select('COUNT(distinct user_id)') + .joins('LEFT JOIN school_daily_reports sdr ON sdr.id = school_daily_active_users.school_daily_report_id') + .where(date_condition_sql).to_sql reports = search_filter(reports) reports.select( 'SUM(teacher_increase_count) teacher_increase_count,'\ @@ -53,7 +57,7 @@ class Management::SchoolDataGrowService 'SUM(shixun_increase_count) shixun_increase_count,'\ 'SUM(shixun_homework_count) shixun_homework_count,'\ 'SUM(shixun_evaluate_count) shixun_evaluate_count,'\ - 'COUNT(distinct au.user_id) uniq_active_user_count,'\ + "(#{subquery}) uniq_active_user_count,"\ 'SUM(active_user_count) active_user_count' ).first end @@ -101,3 +105,4 @@ class Management::SchoolDataGrowService (params[:page].to_i.zero? ? 0 : params[:page].to_i - 1) * PAGE_SIZE end end +# SELECT SUM(teacher_increase_count) teacher_increase_count,SUM(student_increase_count) student_increase_count,SUM(course_increase_count) course_increase_count,SUM(shixun_increase_count) shixun_increase_count,SUM(shixun_homework_count) shixun_homework_count,SUM(shixun_evaluate_count) shixun_evaluate_count,(select count(distinct user_id) from school_daily_active_users sdau left join school_daily_reports sdr on sdr.id = sdau.school_daily_report_id where date BETWEEN '2019-06-02' AND '2019-06-03') uniq_active_user_count,SUM(active_user_count) active_user_count FROM `schools` LEFT JOIN school_daily_reports sdr ON sdr.school_id = schools.id where date BETWEEN '2019-06-02' AND '2019-06-03' LIMIT 1 \ No newline at end of file