Index: trunk/ippTools/src/dettool.c
===================================================================
--- trunk/ippTools/src/dettool.c	(revision 9356)
+++ trunk/ippTools/src/dettool.c	(revision 9369)
@@ -3130,53 +3130,36 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
-    // det_id is required
-    // psString det_id is unused in the first query... it is checked for to
-    // make sure it is in config->where
-    bool status = false;
-    psString det_id = psMetadataLookupStr(&status, config->args, "-det_id");
-    if (!status) {
-        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -det_id");
-        return false;
-    }
-    if (!det_id) {
-        psError(PS_ERR_UNKNOWN, true, "-det_id is required");
-        return false;
-    }
-
-    psArray *normalizedImfiles =
-        detNormalizedImfileSelectRowObjects(config->dbh, config->where, 0);
-    if (!normalizedImfiles) {
+    psString query = psStringCopy(
+        "SELECT detNormalizedImfile.*"
+        " FROM detNormalizedImfile"
+        " JOIN detRun"
+        "   ON detNormalizedImfile.det_id = detRun.position"
+        " 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 detNormalizedImfile rows found");
-        return false;
-    }
-
-    // mask out normalized imfiles that have residuals registered for them
-    psArray *residImfiles = detResidImfileSelectRowObjects(config->dbh,
-            config->where, 0);
-    if (residImfiles) {
-        for (long i = 0; i < psArrayLength(normalizedImfiles); i++) {
-            for (long j = 0; j < psArrayLength(residImfiles); j++) {
-                if (
-                (((detNormalizedImfileRow *)normalizedImfiles->data[i])->det_id
-                    ==
-                ((detResidImfileRow *)residImfiles->data[j])->det_id)
-                &&
-                (((detNormalizedImfileRow *)normalizedImfiles->data[i])->iteration
-                    ==
-                ((detResidImfileRow *)residImfiles->data[j])->iteration)
-                &&
-                (strcmp(
-                ((detNormalizedImfileRow *)normalizedImfiles->data[i])->class_id,
-                ((detResidImfileRow *)residImfiles->data[j])->class_id) == 0))
-                 {
-
-                    // remove the rawImfiles from the list
-                    psArrayRemove(normalizedImfiles, normalizedImfiles->data[i]);
-                    i--;
-                    break;
-                }
-            }
-        }
-        psFree(residImfiles);
+        psFree(output)
+        return true;
     }
 
@@ -3192,13 +3175,11 @@
 
     // negative simple so the default is true
-    if (normalizedImfiles->n) {
-        if (!detNormalizedImfilePrintObjects(stdout, normalizedImfiles, !simple)) {
-            psError(PS_ERR_UNKNOWN, false, "failed to print array");
-            psFree(normalizedImfiles);
-            return false;
-        }
-    }
-
-    psFree(normalizedImfiles);
+    if (!ippdbPrintMetadatas(stdout, output, "detNormalizedImfile", !simple)) {
+        psError(PS_ERR_UNKNOWN, false, "failed to print array");
+        psFree(output);
+        return false;
+    }
+
+    psFree(output);
 
     return true;
