Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8406)
+++ trunk/ippTools/src/dettool.c	(revision 8407)
@@ -2721,30 +2721,40 @@
     psString query = psStringCopy(
         "SELECT DISTINCT"
-        "   detRun.position AS det_id,"
-        "   detRun.iteration,"
-        "   detRun.det_type,"
-        "   detInputExp.exp_id,"
-        "   detInputExp.include"
-        " FROM detRun"
-        " LEFT JOIN detInputExp"
-        "   ON detRun.position = detInputExp.det_id"
-        "   AND detRun.iteration = detInputExp.iteration"
-        " LEFT JOIN rawDetrendExp"
-        "   ON detInputExp.exp_id = rawDetrendExp.exp_id"
-        " LEFT JOIN detResidImfile"
-        "   ON detRun.position = detResidImfile.det_id"
-        "   AND detRun.iteration = detResidImfile.iteration"
-        "   AND detInputExp.exp_id = detResidImfile.exp_id"
-        " LEFT JOIN detResidExp"
-        "   ON detRun.position = detResidExp.det_id"
-        "   AND detRun.iteration = detResidExp.iteration"
-        "   AND detInputExp.exp_id = detResidExp.exp_id"
-        " GROUP BY"
-        "   detInputExp.exp_id"
-        " WHERE"
-        "   detResidExp.det_id IS NULL"
-        "   AND detResidExp.iteration IS NULL"
-        "   AND detResidExp.exp_id IS NULL"
-        " HAVING rawDetrendExp.imfiles = COUNT(detStackedImfile.class_id)"
+        "   det_id,"
+        "   iteration,"
+        "   det_type,"
+        "   exp_id,"
+        "   include"
+        " FROM"
+        "   (SELECT DISTINCT"
+        "       detRun.position AS det_id,"
+        "       detRun.iteration,"
+        "       detRun.det_type,"
+        "       detInputExp.exp_id,"
+        "       detInputExp.include,"
+        "       rawDetrendExp.imfiles"
+        "   FROM detRun"
+        "       LEFT JOIN detInputExp"
+        "       ON detRun.position = detInputExp.det_id"
+        "       AND detRun.iteration = detInputExp.iteration"
+        "   LEFT JOIN rawDetrendExp"
+        "       ON detInputExp.exp_id = rawDetrendExp.exp_id"
+        "   LEFT JOIN detResidImfile"
+        "       ON detRun.position = detResidImfile.det_id"
+        "       AND detRun.iteration = detResidImfile.iteration"
+        "       AND detInputExp.exp_id = detResidImfile.exp_id"
+        "   LEFT JOIN detResidExp"
+        "       ON detRun.position = detResidExp.det_id"
+        "       AND detRun.iteration = detResidExp.iteration"
+        "       AND detInputExp.exp_id = detResidExp.exp_id"
+        "   WHERE"
+        "       detResidExp.det_id IS NULL"
+        "       AND detResidExp.iteration IS NULL"
+        "       AND detResidExp.exp_id IS NULL"
+        "   GROUP BY"
+        "       detInputExp.exp_id"
+        "   HAVING"
+        "       rawDetrendExp.imfiles = COUNT(detResidImfile.class_id)"
+        " ) AS toresidexp"
         ); 
 
