I created 2 custom SQL queries to get my data from my PostgreSQL database. I made two queries with on minor change. The user ID part. with wd.id it returns all worker reports, but sometimes I need to get a specific worker report. Is there any way to combine the two queries?
@Query(value = "SELECT\n" +
"\twd.id as workDetailId,\n" +
" max(ua.name) as name,\n" +
" count(sw.id) as shiftCount,\n" +
" sum(sw.actual_work_duration) as workDuration,\n" +
" sum(sw.actual_shift_duration) as shiftDuration,\n" +
"\twd.salary as salary,\n" +
"\tmax(mi.name) as jobRelation\n" +
"FROM shift_worker sw\n" +
"JOIN user_acc ua ON ua.work_detail_id = sw.work_detail_id\n" +
"JOIN work_detail wd ON sw.work_detail_id = wd.id\n" +
"JOIN maintenance_item mi ON wd.job_relation_id = mi.id\n" +
"WHERE sw.work_detail_id = wd.id AND\n" +
" sw.actual_work_start_time > ?1 AND\n" +
" sw.actual_work_start_time < ?2 AND sw.actual_work_end_time IS NOT NULL\n" +
"GROUP BY wd.id", nativeQuery = true)
Page<UserShiftReport> findUserReports(LocalDateTime startDateTime, LocalDateTime endDateTime, Pageable pageable);
@Query(value = "SELECT\n" +
"\twd.id as workDetailId,\n" +
" max(ua.name) as name,\n" +
" count(sw.id) as shiftCount,\n" +
" sum(sw.actual_work_duration) as workDuration,\n" +
" sum(sw.actual_shift_duration) as shiftDuration,\n" +
"\twd.salary as salary,\n" +
"\tmax(mi.name) as jobRelation\n" +
"FROM shift_worker sw\n" +
"JOIN user_acc ua ON ua.work_detail_id = sw.work_detail_id\n" +
"JOIN work_detail wd ON sw.work_detail_id = wd.id\n" +
"JOIN maintenance_item mi ON wd.job_relation_id = mi.id\n" +
"WHERE sw.work_detail_id = ?1 AND\n" +
" sw.actual_work_start_time > ?2 AND\n" +
" sw.actual_work_start_time < ?3 AND sw.actual_work_end_time IS NOT NULL\n" +
"GROUP BY wd.id", nativeQuery = true)
Page<UserShiftReport> findUserReportByUserId(Long userId, LocalDateTime startDateTime, LocalDateTime endDateTime, Pageable pageable);