From 23ef2e14104551c7ac5fac4deb3e9c7c2c48ee6f Mon Sep 17 00:00:00 2001
From: Leo Gordon <lg4@ebi.ac.uk>
Date: Thu, 15 Aug 2013 16:18:49 +0100
Subject: [PATCH] =?UTF-8?q?Bugfixes=20to=20pacify=20pgsql:=20changed=20a?=
 =?UTF-8?q?=20non-functional=20"HAVING"=20into=20a=20nested=20SELECT,=20an?=
 =?UTF-8?q?d=20changed=20unsupported=20SUM()=20into=20COUNT(CASE=20?=
 =?UTF-8?q?=E2=80=A6=20)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../EnsEMBL/Hive/DBSQL/AnalysisJobAdaptor.pm  | 21 ++++++++++---------
 1 file changed, 11 insertions(+), 10 deletions(-)

diff --git a/modules/Bio/EnsEMBL/Hive/DBSQL/AnalysisJobAdaptor.pm b/modules/Bio/EnsEMBL/Hive/DBSQL/AnalysisJobAdaptor.pm
index c30e4d6fe..ace5687b8 100644
--- a/modules/Bio/EnsEMBL/Hive/DBSQL/AnalysisJobAdaptor.pm
+++ b/modules/Bio/EnsEMBL/Hive/DBSQL/AnalysisJobAdaptor.pm
@@ -700,16 +700,17 @@ sub balance_semaphores {
     my ($self, $filter_analysis_id) = @_;
 
     my $find_sql    = qq{
-                        SELECT funnel.job_id, funnel.semaphore_count was, COALESCE(SUM(fan.status!='DONE' AND fan.status!='PASSED_ON'),0) should
-                        FROM job funnel
-                        LEFT JOIN job fan ON (funnel.job_id=fan.semaphored_job_id)
-                        WHERE }
-                    .($filter_analysis_id ? "funnel.analysis_id=$filter_analysis_id AND " : '')
-                    .qq{
-                        funnel.status='SEMAPHORED'
-                        GROUP BY funnel.job_id
-                        HAVING was<>should OR should=0
-                    };
+                        SELECT * FROM (
+                            SELECT funnel.job_id, funnel.semaphore_count AS was, COALESCE(COUNT(CASE WHEN fan.status!='DONE' AND fan.status!='PASSED_ON' THEN 1 ELSE NULL END),0) AS should
+                            FROM job funnel
+                            LEFT JOIN job fan ON (funnel.job_id=fan.semaphored_job_id)
+                            WHERE }
+                        .($filter_analysis_id ? "funnel.analysis_id=$filter_analysis_id AND " : '')
+                        .qq{
+                            funnel.status='SEMAPHORED'
+                            GROUP BY funnel.job_id
+                         ) AS internal WHERE was<>should OR should=0
+                     };
 
     my $update_sql  = "UPDATE job SET "
         ." semaphore_count=? , "
-- 
GitLab