INSERT INTO FinishLessonStat ( uid, unitId, content, lessonCount, completeCount, created, howmuch, maxScore, avgScore , areaid ) Values ( #{uid}, #{unitId}, #{content}, #{lessonCount}, #{completeCount},current_timestamp, #{howmuch}, #{maxScore}, #{avgScore} , #{areaid} ) UPDATE FinishLessonStat SET uid = #{uid}, unitId = #{unitId}, content = #{content}, lessonCount = #{lessonCount}, completeCount = #{completeCount}, created = #{created}, howmuch = #{howmuch}, maxScore = #{maxScore}, avgScore = #{avgScore} , areaid = #{areaid} WHERE uid=#{uid} and areaid = #{areaid} with temp as ( select distinct a.uid , b.parentId as unitId , a.areaId from ( select uid , objectId , areaId from Achivement where achivetype = 'L' and pointcasetype in ( 5, 6 , 7 , 8 ) and created > current_date - interval '1 day' and created < current_date ) as a inner join Lesson b on a.objectId = b.lessonId ) DELETE FROM FinishLessonStat m USING temp WHERE m.uid = temp.uid and m.unitId = temp.UnitId with temp as ( select distinct a.uid , b.parentId as unitId , a.areaId from ( select uid , objectId , areaId from Achivement where achivetype = 'L' and pointcasetype in ( 5, 6 , 7 , 8 ) and created > current_date - interval '1 day' and created < current_date ) as a inner join Lesson b on a.objectId = b.lessonId ) insert into FinishLessonStat ( uid , unitid , content , lessoncount , completecount , created , howmuch , maxscore , avgscore , areaId ) select p.uid , p.unitid , s.lessonname as content ,r.lessoncount , p.completecount , current_timestamp , p.howmuch , p.maxscore , p.avgscore ,p.areaId from ( select temp.uid , temp.unitId , temp.areaid , count( distinct a.objectId ) as completecount, count(a.*) as howmuch , max(a.score ) as maxscore , round(avg(a.score)) as avgscore from temp inner join Lesson b on temp.unitId = b.parentId inner join Achivement a on temp.uid = a.uid and b.lessonId = a.objectId and a.achivetype = 'L' and a.pointCaseType in ( 5,6 ,7 ,8) group by temp.uid , temp.unitId , temp.areaId ) as p inner join ( select parentId as unitId , count(*) as lessonCount from lesson where LessonType = 'L' and status = 1 group by parentId ) as r on p.unitId = r.unitid inner join Lesson s on p.unitId = s.lessonId