Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 8460)
+++ trunk/ippTools/src/dettool.c	(revision 8462)
@@ -3220,4 +3220,88 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+    /* which returns a list of detrend runs (with detrend id, iteration and
+     * detrend type) which have completed all residexps.
+     */
+ 
+    // select detRun.position
+    // select detRun.iteration
+    // select detRun.det_type
+    // by:
+    // find the current iteration bassed on det_id
+    // find all exp_ids in the current det_id/iteration from detInputExp
+    // find all exp_ids in the current det_id/iteration from detResidExp
+    // compare the counts of exp_ids
+
+    psString query = psStringCopy(
+        "SELECT DISTINCT"
+        "   det_id,"
+        "   iteration,"
+        "   det_type"
+        " FROM"
+        "   (SELECT DISTINCT"
+        "       detRun.position AS det_id,"
+        "       detRun.iteration,"
+        "       detRun.det_type,"
+        "       detInputExp.exp_id"
+        "   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 detResidExp"
+        "       ON detRun.position = detResidExp.det_id"
+        "       AND detRun.iteration = detResidExp.iteration"
+        "       AND detInputExp.exp_id = detResidExp.exp_id"
+        "   GROUP BY"
+        "       detRun.position,"
+        "       detRun.iteration"
+        "   HAVING"
+        "       COUNT(detResidExp.exp_id) = COUNT(detInputExp.exp_id)"
+        " ) AS residdetrun"
+        ); 
+
+    // XXX does it make sens to accept any search params?
+#if 0
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+#endif
+
+    if (!p_psDBRunQuery(config->dbh, query)) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        psFree(query);
+        return false;
+    } 
+    psFree(query);
+
+    psArray *output = p_psDBFetchResult(config->dbh);
+    if (!output) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no pending rawDetrendExp rows found");
+        return false;
+    }
+
+    bool simple = false;
+    {
+        bool status = false;
+        simple = psMetadataLookupBool(&status, config->args, "-simple");
+        if (!status) {
+            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
+            return false;
+        }
+    }
+
+    // negative simple so the default is true
+    if (!ippdbPrintMetadatas(stdout, output, "rawDetrendImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
+
     return true;
 }
