Index: /trunk/ippTools/src/dettool.c
===================================================================
--- /trunk/ippTools/src/dettool.c	(revision 9175)
+++ /trunk/ippTools/src/dettool.c	(revision 9176)
@@ -804,9 +804,10 @@
             " JOIN detInputExp USING(exp_tag) "
             " JOIN detRun ON detInputExp.det_id = detRun.position"
+            " WHERE detRun.state = 'run'"
         );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereSQL(config->where, "rawImfile");
-        psStringAppend(&query, " %s", whereClause);
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawImfile");
+        psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
@@ -876,5 +877,6 @@
                 " AND rawImfile.class_id = detProcessedImfile.class_id"
             " WHERE"
-            " detInputExp.det_id IS NOT NULL"
+            " detRun.state = 'run'"
+            " AND detInputExp.det_id IS NOT NULL"
             " AND detInputExp.exp_tag IS NOT NULL"
             " AND detProcessedImfile.det_id IS NULL"
@@ -1153,7 +1155,8 @@
         "    AND detProcessedImfile.class_id = rawImfile.class_id"
         " WHERE"
-        "    detProcessedExp.det_id IS NULL"
-        "    AND detProcessedExp.exp_tag IS NULL"
-        "    AND detInputExp.include = 1"
+        "   detRun.state = 'run'"
+        "   AND  detProcessedExp.det_id IS NULL"
+        "   AND detProcessedExp.exp_tag IS NULL"
+        "   AND detInputExp.include = 1"
         " GROUP BY"
         "    detProcessedImfile.class_id,"
@@ -1305,9 +1308,10 @@
         "    AND detProcessedImfile.class_id = rawImfile.class_id"
         " WHERE"
-        "    detProcessedExp.det_id IS NULL"
-        "    AND detProcessedExp.exp_tag IS NULL"
-        "    AND detInputExp.include = 1"
-        "    AND detRun.position = %s"
-        "    AND detProcessedImfile.exp_tag = '%s'"
+        "   detRun.state = 'run'"
+        "   AND detProcessedExp.det_id IS NULL"
+        "   AND detProcessedExp.exp_tag IS NULL"
+        "   AND detInputExp.include = 1"
+        "   AND detRun.position = %s"
+        "   AND detProcessedImfile.exp_tag = '%s'"
         " GROUP BY"
         "    detProcessedImfile.class_id,"
@@ -1451,8 +1455,9 @@
         "    AND detProcessedImfile.class_id = detStackedImfile.class_id"
         " WHERE"
-        "    detStackedImfile.det_id IS NULL"
-        "    AND detStackedImfile.iteration IS NULL"
-        "    AND detStackedImfile.class_id IS NULL"
-        "    AND detInputExp.include = 1"
+        "   detRun.state = 'run'"
+        "   AND detStackedImfile.det_id IS NULL"
+        "   AND detStackedImfile.iteration IS NULL"
+        "   AND detStackedImfile.class_id IS NULL"
+        "   AND detInputExp.include = 1"
         " GROUP BY"
         "    rawImfile.class_id"
@@ -1517,9 +1522,11 @@
         " JOIN detRun"
         "   ON detProcessedImfile.det_id = detRun.position"
+        " WHERE"
+        "   detRun.state = 'run'"
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereSQL(config->where, NULL);
-        psStringAppend(&query, " %s", whereClause);
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
@@ -2134,4 +2141,6 @@
         "     ON detInputExp.det_id = detStackedImfile.det_id"
         "     AND detInputExp.iteration = detStackedImfile.iteration"
+        " WHERE"
+        "   detRun.state = 'run'"
         " GROUP BY"
         "     rawDetrendExp.exp_tag"
@@ -2609,7 +2618,8 @@
         "    AND detInputExp.iteration = detNormalizedExp.iteration"
         " WHERE"
-        "    detNormalizedExp.det_id IS NULL"
-        "    AND detNormalizedExp.iteration IS NULL"
-        "    AND detInputExp.include = 1"
+        "   detRun.state = 'run'"
+        "   AND detNormalizedExp.det_id IS NULL"
+        "   AND detNormalizedExp.iteration IS NULL"
+        "   AND detInputExp.include = 1"
         " GROUP BY"
         "    detNormalizedImfile.iteration,"
@@ -2763,9 +2773,10 @@
         "    AND detInputExp.iteration = detNormalizedExp.iteration"
         " WHERE"
-        "    detNormalizedExp.det_id IS NULL"
-        "    AND detNormalizedExp.iteration IS NULL"
-        "    AND detInputExp.include = 1"
-        "    AND detRun.position = %s"
-        "    AND detNormalizedImfile.iteration = %d"
+        "   detRun.state = 'run'"
+        "   AND detNormalizedExp.det_id IS NULL"
+        "   AND detNormalizedExp.iteration IS NULL"
+        "   AND detInputExp.include = 1"
+        "   AND detRun.position = %s"
+        "   AND detNormalizedImfile.iteration = %d"
         " GROUP BY"
         "    detNormalizedImfile.iteration,"
@@ -2933,5 +2944,6 @@
         "   AND detProcessedImfile.class_id = detResidImfile.class_id"
         " WHERE"
-        "   detResidImfile.det_id IS NULL"
+        "   detRun.state = 'run'"
+        "   AND detResidImfile.det_id IS NULL"
         "   AND detResidImfile.iteration IS NULL"
         "   AND detResidImfile.exp_tag IS NULL"
@@ -3517,5 +3529,6 @@
         "       AND detInputExp.exp_tag = detResidExp.exp_tag"
         "   WHERE"
-        "       detResidExp.det_id IS NULL"
+        "       detRun.state = 'run'"
+        "       AND detResidExp.det_id IS NULL"
         "       AND detResidExp.iteration IS NULL"
         "       AND detResidExp.exp_tag IS NULL"
@@ -3625,5 +3638,6 @@
         "       AND detInputExp.exp_tag = detResidExp.exp_tag"
         "   WHERE"
-        "       detResidExp.det_id IS NULL"
+        "       detrun.state = 'run'"
+        "       AND detResidExp.det_id IS NULL"
         "       AND detResidExp.iteration IS NULL"
         "       AND detResidExp.exp_tag IS NULL"
@@ -3929,4 +3943,6 @@
         "       AND detRun.iteration = detResidExp.iteration"
         "       AND detInputExp.exp_tag = detResidExp.exp_tag"
+        "   WHERE"
+        "       detrun.state = 'run'"
         "   GROUP BY"
         "       detRun.position,"
@@ -4185,4 +4201,6 @@
         "       AND detRun.iteration = detResidExp.iteration"
         "       AND detInputExp.exp_tag = detResidExp.exp_tag"
+        "   WHERE"
+        "       detrun.state = 'run'"
         "   GROUP BY"
         "       detRun.position,"
@@ -4423,5 +4441,6 @@
         "   AND detInputExp.exp_tag = detResidExp.exp_tag"
         " WHERE"
-        "   detInputExp.exp_tag IS NOT NULL"
+        "   detRun.state = 'run'"
+        "   AND detInputExp.exp_tag IS NOT NULL"
         "   AND detResidExp.accept IS NOT NULL"
         " GROUP BY"
