Index: trunk/ippTools/src/detselect.c
===================================================================
--- trunk/ippTools/src/detselect.c	(revision 9343)
+++ trunk/ippTools/src/detselect.c	(revision 9346)
@@ -51,15 +51,29 @@
 
     psString query = psStringCopy(
-            "SELECT p3PendingExp.*"
-            " FROM p3PendingExp"
-            " LEFT JOIN p3ProcessedExp"
-            "   USING(exp_tag)"
-            " WHERE"
-            "   p3ProcessedExp.exp_tag IS NULL"
+        "SELECT"
+        " detNormalizedImfile.det_id,"
+        " detNormalizedImfile.iteration,"
+        " detNormalizedImfile.class_id"
+        " FROM detRun"
+        " JOIN detInputExp"
+        // can't join on the iteration here as we want the results from all
+        // iterations
+        "   ON detRun.position = detInputExp.det_id"
+        " JOIN detNormalizedImfile"
+        "   ON detInputExp.position = detNormalizedImfile.det_id"
+        "   AND detInputExp.iteration = detNormalizedImfile.iteration"
+        " JOIN rawDetrendExp"
+        "   ON detInputExp.exp_tag = rawDetrendExp.exp_tag"
+        " JOIN detRunSummary"
+        "   ON detInputExp.position = detRunSummary.det_id"
+        "   AND detInputExp.iteration = detRunSummary.iteration"
+        " WHERE"
+        "   detRun.state = 'stop'"
+        "   AND detRunSummary.accept = 1" 
     );
 
     if (config->where) {
-        psString whereClause = psDBGenerateWhereConditionSQL(config->where, NULL);
-        psStringAppend(&query, " %s", whereClause);
+        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawDetrendExp");
+        psStringAppend(&query, " AND %s", whereClause);
         psFree(whereClause);
     }
@@ -79,5 +93,5 @@
     if (!psArrayLength(output)) {
         // XXX check psError here
-        psError(PS_ERR_UNKNOWN, false, "no p3PendingExp rows found");
+        psError(PS_ERR_UNKNOWN, false, "no detrend exposures found");
         psFree(output);
         return true;
@@ -95,5 +109,5 @@
 
     // negate simple so the default is true
-    if (!ippdbPrintMetadatas(stdout, output, "p3PendingExp", !simple)) {
+    if (!ippdbPrintMetadatas(stdout, output, "detExp", !simple)) {
         psError(PS_ERR_UNKNOWN, false, "failed to print array");
         psFree(output);
