Index: /trunk/ippTools/src/detselect.c
===================================================================
--- /trunk/ippTools/src/detselect.c	(revision 9405)
+++ /trunk/ippTools/src/detselect.c	(revision 9406)
@@ -69,4 +69,6 @@
     PS_ASSERT_PTR_NON_NULL(config, false);
 
+# define JOSH_SQL 0
+# if (JOSH_SQL)
     psString query = psStringCopy(
         "SELECT"
@@ -90,4 +92,31 @@
         "   AND detRunSummary.accept = 1" 
     );
+# else
+    psString query = psStringCopy(
+        "SELECT DISTINCT"
+        "   detRunSummary.det_id,"
+        "   detRunSummary.iteration,"
+        "   rawImfile.class,"
+        "   detRun.det_type"
+        // "   detRun.state,"
+        // "   detRunSummary.accept,"
+        // "   rawDetrendExp.camera"
+	"  FROM detRun"
+	"  JOIN detRunSummary "
+	"    ON detRun.position = detRunSummary.det_id"
+	"  JOIN detInputExp"
+	"    ON detRunSummary.det_id = detInputExp.det_id"
+	"   AND detRunSummary.iteration = detInputExp.iteration"
+	"  JOIN rawImfile"
+	"    ON detInputExp.exp_tag = rawImfile.exp_tag"
+	"  JOIN rawDetrendExp"
+	"    ON detInputExp.exp_tag = rawDetrendExp.exp_tag"
+	" WHERE detRun.state = 'stop'"
+	"   AND detRunSummary.accept = 1"
+	// XXX the following entry is needed to exclude db mismatched
+	// XXX these are caused by an error upstream and should be fixed
+        "   AND detRun.det_type = rawDetrendExp.object"
+    );
+# endif
 
     if (config->where) {
@@ -105,5 +134,5 @@
     }
 
-    psStringAppend(&query, " LIMIT 1");
+    // psStringAppend(&query, " LIMIT 1");
 
     if (!p_psDBRunQuery(config->dbh, query)) {
