Merge pull request 'fix(defaulter_filter): recovery subquery' (!2849) from hotFix_defaulterFilter_recovery into master
gitea/salix/pipeline/head This commit looks good Details

Reviewed-on: #2849
Reviewed-by: Javi Gallego <jgallego@verdnatura.es>
This commit is contained in:
Alex Moreno 2024-08-13 06:54:59 +00:00
commit f28631c376
1 changed files with 8 additions and 3 deletions

View File

@ -83,9 +83,14 @@ module.exports = Self => {
LEFT JOIN account.user u ON u.id = c.salesPersonFk
LEFT JOIN account.user uw ON uw.id = co.workerFk
LEFT JOIN (
SELECT MAX(started), clientFk, finished
FROM recovery
GROUP BY clientFk
SELECT r1.started, r1.clientFk, r1.finished
FROM recovery r1
JOIN (
SELECT MAX(started) AS maxStarted, clientFk
FROM recovery
GROUP BY clientFk
) r2 ON r1.clientFk = r2.clientFk
AND r1.started = r2.maxStarted
) r ON r.clientFk = c.id
LEFT JOIN workerDepartment wd ON wd.workerFk = u.id
JOIN department dp ON dp.id = wd.departmentFk