Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 9274)
+++ trunk/ippTools/src/dettool.c	(revision 9283)
@@ -4007,10 +4007,41 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // no options are required... use the default where statement
-    psArray *residExps = detResidExpSelectRowObjects(config->dbh,
-            config->where, 0);
-    if (!residExps) {
-        psError(PS_ERR_UNKNOWN, false, "no detResidExpg rows found");
-        return false;
+    psString query = psStringCopy(
+            "SELECT"
+            "   detResidExp.*,"
+            "   detInputExp.include"
+            " FROM detResidExp"
+            " JOIN detInputExp"
+            "   USING(det_id, exp_tag, iteration)"
+            " JOIN detRun"
+            "   ON detResidExp.det_id = detRun.position"
+            "   AND detResidExp.iteration = detRun.iteration"
+            " WHERE"
+            "   detRun.state = 'run'"
+    );
+
+    if (config->where) {
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
+        psStringAppend(&query, " AND %s", whereClause);
+        psFree(whereClause);
+    }
+
+    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) {
+        psError(PS_ERR_UNKNOWN, false, "database error");
+        return false;
+    }
+    if (!psArrayLength(output)) {
+        // XXX check psError here
+        psError(PS_ERR_UNKNOWN, false, "no detResidExp rows found");
+        psFree(output)
+        return true;
     }
 
@@ -4026,13 +4057,11 @@
 
     // negative simple so the default is true
-    if (residExps->n) {
-        if (!detResidExpPrintObjects(stdout, residExps, !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(residExps);
-            return false;
-        }
-    }
-
-    psFree(residExps);
+    if (!ippdbPrintMetadatas(stdout, output, "detResidExp", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
 
     return true;
