Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 8352)
+++ /trunk/ippTools/src/dettool.c	(revision 8353)
@@ -1656,14 +1656,13 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    // select detRun.position (det_id)
     // select detRun.iteration
-    // select detProcessedImfile.det_id
-    // select detProcessedImfile.class_id
     // by:
     // find the current iteration bassed on det_id
     // find all exp_ids in the current det_id/iteration from detInputExp
-    // find all rawImfiles in the current exp_ids
-    // compare to detProcessedImfiles by det_id/exp_id
-    // found how many imfile there are in each class_id
+    // sort to detInputExp.imfiles to find the largest value per det_id/iter
+    // compare imfiles to the number of detStackedImfiles by class_id
     // and: 
+    // ???
     // det_id is not in detStackedImfile;
     // iteration is not in detStackedImfile;
@@ -1671,26 +1670,21 @@
 
     psString query = psStringCopy(
-        " SELECT"
-        "    detRun.position,"
-        "    detRun.iteration,"
+        "SELECT DISTINCT"
+        "     detRun.position AS det_id,"
+        "     detRun.iteration"
         " FROM detRun"
         " LEFT JOIN detInputExp"
-        "    ON detRun.position = detInputExp.det_id"
-        "    AND detRun.iteration = detInputExp.iteration"
+        "     ON detRun.position = detInputExp.det_id"
+        "     AND detRun.iteration = detInputExp.iteration"
+        " LEFT JOIN rawDetrendExp"
+        "     ON detInputExp.exp_id = rawDetrendExp.exp_id"
         " LEFT JOIN detStackedImfile"
-        "    ON detInputExp.det_id = detStackedImfile.det_id"
-        "    AND detInputExp.iteration = detStackedImfile.iteration"
-        " LEFT JOIN rawImfile"
-        "    ON detInputExp.exp_id = rawImfile.exp_id"
-        " WHERE"
-        "    detStackedImfile.det_id IS NOT NULL"
-        "    AND detStackedImfile.iteration IS NOT NULL"
-        "    AND detStackedImfile.class_id IS NOT NULL"
+        "     ON detInputExp.det_id = detStackedImfile.det_id"
+        "     AND detInputExp.iteration = detStackedImfile.iteration"
         " GROUP BY"
-        "    detStackedImfile.class_id,"
-        "    rawImfile.class_id"
-        " HAVING"
-        "    COUNT(detStackedImfile.class_id) = COUNT(rawImfile.class_id)"
+        "     rawDetrendExp.imfiles"
+        " HAVING MAX(rawDetrendExp.imfiles) = COUNT(detStackedImfile.class_id)"
         ); 
+
 
     // XXX does it make sens to accept any search params?
