Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8590)
+++ trunk/ippTools/src/dettool.c	(revision 8606)
@@ -2558,8 +2558,12 @@
     // where det_id, iteration, class_id is not in detResidImfile
     psString query = psStringCopy(
-        "SELECT"
-        "   detNormalizedImfile.*"
+        "SELECT * FROM"
+        " (SELECT"
+        "   detNormalizedImfile.det_id,"
+        "   detNormalizedImfile.iteration,"
+        "   detNormalizedImfile.class_id,"
+        "   detNormalizedImfile.uri"
         " FROM detNormalizedImfile"
-        " LEFT JOIN detInputExp"
+        " JOIN detInputExp"
         "   USING(det_id, iteration)"
         " LEFT JOIN detResidImfile"
@@ -2569,9 +2573,31 @@
         "   AND detNormalizedImfile.class_id = detResidImfile.class_id"
         " WHERE"
-        "  detInputExp.exp_id = '%s'" 
-        "  AND detResidImfile.det_id IS NULL" 
+        "  detResidImfile.det_id IS NULL" 
         "  AND detResidImfile.iteration IS NULL" 
         "  AND detResidImfile.exp_id IS NULL" 
         "  AND detResidImfile.class_id IS NULL" 
+        "  AND detInputExp.exp_id = '%s'" 
+        " UNION"
+        " SELECT" 
+        "   detStackedImfile.det_id,"
+        "   detStackedImfile.iteration,"
+        "   detStackedImfile.class_id,"
+        "   detStackedImfile.uri"
+        " FROM detStackedImfile"
+        " JOIN detInputExp"
+        "   USING(det_id, iteration)"
+        " LEFT JOIN detResidImfile"
+        "   ON detStackedImfile.det_id = detResidImfile.det_id"
+        "   AND detStackedImfile.iteration= detResidImfile.iteration"
+        "   AND detInputExp.exp_id = detResidImfile.exp_id"
+        "   AND detStackedImfile.class_id = detResidImfile.class_id"
+        " WHERE"
+        "   detResidImfile.det_id IS NULL"  
+        "   AND detResidImfile.iteration IS NULL"  
+        "   AND detResidImfile.exp_id IS NULL"  
+        "   AND detResidImfile.class_id IS NULL"  
+        "   AND detInputExp.exp_id = '%s'" 
+        "   AND detStackedImfile.normalize = 0"
+        " ) as detNormStackedImfile"
         );
 
@@ -2626,8 +2652,8 @@
 
         // there's not
-        psString whereClause = psDBGenerateWhereConditionSQL(where, "detNormalizedImfile");
+        psString whereClause = psDBGenerateWhereSQL(where, NULL);
         psFree(where);
         if (whereClause) {
-            psStringAppend(&query, " AND %s", whereClause);
+            psStringAppend(&query, " %s", whereClause);
             psFree(whereClause);
         }
