Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 14058)
+++ trunk/ippTools/share/Makefile.am	(revision 14059)
@@ -14,9 +14,11 @@
 	dettool_find_completed_runs.sql \
 	dettool_raw.sql \
+	dettool_tonormalizedexp.sql \
+	dettool_tonormalizedstat.sql \
+	dettool_toprocessedexp.sql \
     dettool_toprocessedimfile.sql \
-	dettool_toprocessedexp.sql \
+	dettool_toresidexp.sql \
+	dettool_toresidimfile.sql \
 	dettool_tostacked.sql \
-	dettool_toresidimfile.sql \
-	dettool_toresidexp.sql \
 	difftool_inputskyfile.sql \
 	difftool_skyfile.sql \
Index: trunk/ippTools/share/dettool_tonormalizedexp.sql
===================================================================
--- trunk/ippTools/share/dettool_tonormalizedexp.sql	(revision 14059)
+++ trunk/ippTools/share/dettool_tonormalizedexp.sql	(revision 14059)
@@ -0,0 +1,41 @@
+SELECT DISTINCT
+    *
+FROM
+    (SELECT DISTINCT
+        detRun.det_id as det_id,
+        detRun.iteration,
+        detRun.det_type,
+        detRun.workdir,
+        rawExp.camera,
+        rawExp.telescope,
+        rawExp.exp_type,
+        detNormalizedImfile.class_id,
+        rawImfile.class_id as rawimfile_class_id
+    FROM detRun
+    JOIN detInputExp
+        ON detRun.det_id = detInputExp.det_id
+        AND detRun.iteration = detInputExp.iteration
+    JOIN rawExp
+        ON detInputExp.exp_id = rawExp.exp_id
+    JOIN detNormalizedImfile
+        ON detInputExp.det_id = detNormalizedImfile.det_id
+        AND detInputExp.iteration = detNormalizedImfile.iteration
+    LEFT JOIN rawImfile
+        ON detInputExp.exp_id = rawImfile.exp_id
+        AND detNormalizedImfile.class_id = rawImfile.class_id
+    LEFT JOIN detNormalizedExp
+        ON detInputExp.det_id = detNormalizedExp.det_id
+        AND detInputExp.iteration = detNormalizedExp.iteration
+    WHERE
+        detRun.state = 'run'
+        AND detRun.mode = 'master'
+        AND detNormalizedImfile.fault = 0
+        AND detNormalizedExp.det_id IS NULL
+        AND detNormalizedExp.iteration IS NULL
+        AND detInputExp.include = 1
+    GROUP BY
+        detNormalizedImfile.iteration,
+        detRun.det_id
+    HAVING
+        COUNT(detNormalizedImfile.class_id) = COUNT(rawImfile.class_id)
+    ) as Foo
Index: trunk/ippTools/share/dettool_tonormalizedstat.sql
===================================================================
--- trunk/ippTools/share/dettool_tonormalizedstat.sql	(revision 14059)
+++ trunk/ippTools/share/dettool_tonormalizedstat.sql	(revision 14059)
@@ -0,0 +1,56 @@
+-- select detRun.det_id (det_id)
+-- select detRun.iteration
+-- by:
+-- find the current iteration bassed on det_id
+-- find all exp_ids in the current det_id/iteration from detInputExp
+-- sort to detInputExp.imfiles to find the largest value per det_id/iter
+-- compare imfiles to the number of detStackedImfiles by class_id
+-- and:
+-- ???
+-- det_id is not in detStackedImfile;
+-- iteration is not in detStackedImfile;
+
+SELECT
+    det_id,
+    det_type,
+    iteration,
+    camera,
+    workdir
+FROM
+    (SELECT DISTINCT
+        detRun.det_id,
+        detRun.det_type,
+        detRun.iteration,
+        detRun.workdir,
+        rawExp.camera,
+        detStackedImfile.class_id,
+        rawImfile.class_id as rawimfile_class_id
+    FROM detRun
+    JOIN detInputExp
+        ON detRun.det_id = detInputExp.det_id
+        AND detRun.iteration = detInputExp.iteration
+    JOIN rawExp
+        ON detInputExp.exp_id = rawExp.exp_id
+    JOIN rawImfile
+        ON rawExp.exp_id = rawImfile.exp_id
+    LEFT JOIN detStackedImfile
+        ON detInputExp.det_id = detStackedImfile.det_id
+        AND detInputExp.iteration = detStackedImfile.iteration
+        AND rawImfile.class_id = detStackedImfile.class_id
+    LEFT JOIN detNormalizedStatImfile
+        ON detStackedImfile.det_id = detNormalizedStatImfile.det_id
+        AND detStackedImfile.iteration = detNormalizedStatImfile.iteration
+        AND detStackedImfile.class_id = detNormalizedStatImfile.class_id
+    WHERE
+        detRun.state = 'run'
+        AND detRun.mode = 'master'
+        AND detStackedImfile.fault = 0
+        AND detNormalizedStatImfile.det_id IS NULL
+        AND detNormalizedStatImfile.iteration IS NULL
+        AND detNormalizedStatImfile.class_id IS NULL
+    GROUP BY
+--        rawExp.exp_id,
+        detRun.iteration,
+        detRun.det_id
+    HAVING COUNT(rawImfile.class_id) = COUNT(detStackedImfile.class_id)
+    ) as tonormalizedstat
Index: trunk/ippTools/share/dettool_toprocessedexp.sql
===================================================================
--- trunk/ippTools/share/dettool_toprocessedexp.sql	(revision 14058)
+++ trunk/ippTools/share/dettool_toprocessedexp.sql	(revision 14059)
@@ -29,4 +29,5 @@
         detProcessedImfile.exp_id,
         detProcessedImfile.class_id,
+        rawImfile.class_id as rawimfile_class_id,
         rawExp.camera,
         rawExp.exp_tag
@@ -56,5 +57,5 @@
         detRun.det_id
     HAVING
-        COUNT(detProcessedImfile.class_id) = rawExp.imfiles
+        COUNT(detProcessedImfile.class_id) = COUNT(rawImfile.class_id)
     ) AS detProcessedExp
 
Index: trunk/ippTools/share/dettool_toresidexp.sql
===================================================================
--- trunk/ippTools/share/dettool_toresidexp.sql	(revision 14058)
+++ trunk/ippTools/share/dettool_toresidexp.sql	(revision 14059)
@@ -36,5 +36,4 @@
         detInputExp.include,
         rawExp.camera,
-        rawExp.imfiles,
         rawExp.exp_tag,
         detResidImfile.class_id
@@ -44,8 +43,11 @@
     JOIN rawExp
         ON detInputExp.exp_id = rawExp.exp_id
-    JOIN detResidImfile
+    JOIN rawImfile
+        on rawExp.exp_id = rawImfile.exp_id
+    LEFT JOIN detResidImfile
         ON detRun.det_id = detResidImfile.det_id
         AND detRun.iteration = detResidImfile.iteration
         AND detInputExp.exp_id = detResidImfile.exp_id
+        AND rawImfile.class_id = detResidImfile.class_id
     LEFT JOIN detResidExp
         ON detResidImfile.det_id = detResidExp.det_id
@@ -63,4 +65,4 @@
         detRun.det_id
     HAVING
-        rawExp.imfiles = COUNT(detResidImfile.class_id)
+        COUNT(rawImfile.class_id) = COUNT(detResidImfile.class_id)
     ) AS toresidexp
