Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 26915)
+++ trunk/ippTools/share/Makefile.am	(revision 26927)
@@ -100,4 +100,6 @@
      difftool_definewarpwarp_select.sql \
      difftool_definewarpwarp_insert.sql \
+     difftool_definestackstack_part0.sql \
+     difftool_definestackstack_part1.sql \
      difftool_donecleanup.sql \
      difftool_export_input_skyfile.sql \
Index: trunk/ippTools/share/difftool_definestackstack_part0.sql
===================================================================
--- trunk/ippTools/share/difftool_definestackstack_part0.sql	(revision 26927)
+++ trunk/ippTools/share/difftool_definestackstack_part0.sql	(revision 26927)
@@ -0,0 +1,41 @@
+SELECT DISTINCT
+       stackRun.data_group AS INPUT_data_group,
+       stackRun.dist_group AS INPUT_dist_group,
+       stackRun.filter AS INPUT_filter,
+       stackRun.label AS INPUT_label,
+       stackRun.tess_id AS INPUT_tess_id
+FROM stackRun JOIN stackSumSkyfile USING(stack_id) 
+JOIN (
+     SELECT 
+     	    MAX(stackRun.stack_id) as max_stack_id,
+	    stackRun.filter,
+	    stackRun.label as template_label,
+	    stackRun.tess_id,
+	    stackRun.skycell_id,
+	    stackSumSkyfile.good_frac
+     FROM stackRun JOIN stackSumSkyfile USING(stack_id)
+     WHERE 
+     	   stackRun.state = 'full' AND
+	   stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
+	   -- template where hook %s
+     GROUP BY
+     	   skycell_id,
+	   filter
+) AS template ON stackRun.filter = template.filter 
+              AND stackRun.tess_id = template.tess_id 
+	      AND stackRun.skycell_id = template.skycell_id 
+LEFT JOIN (
+     SELECT 
+     	    diffRun.diff_id,
+	    stackRun.stack_id 
+     FROM diffRun JOIN diffInputSkyfile USING(diff_id) 
+     	  JOIN stackRun ON stackRun.stack_id = diffInputSkyfile.stack1 
+	  JOIN stackSumSkyfile USING(stack_id)
+	  WHERE 1
+	  -- diff where hook %s
+	  -- input where hook %s
+) AS diffExp ON stackRun.stack_id = diffExp.stack_id 
+WHERE
+	stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
+	-- input where hook again %s
+ORDER BY stackRun.data_group,stackRun.filter
Index: trunk/ippTools/share/difftool_definestackstack_part1.sql
===================================================================
--- trunk/ippTools/share/difftool_definestackstack_part1.sql	(revision 26927)
+++ trunk/ippTools/share/difftool_definestackstack_part1.sql	(revision 26927)
@@ -0,0 +1,46 @@
+SELECT 
+       stackRun.stack_id,
+       stackRun.data_group,
+       stackRun.filter,
+       stackRun.label,
+       stackRun.tess_id,
+       stackRun.skycell_id,
+       stackSumSkyfile.good_frac,
+       template.max_stack_id,
+       template.template_label,
+       diffExp.diff_id
+FROM stackRun JOIN stackSumSkyfile USING(stack_id) 
+JOIN (
+     SELECT 
+     	    MAX(stackRun.stack_id) as max_stack_id,
+	    stackRun.filter,
+	    stackRun.label as template_label,
+	    stackRun.tess_id,
+	    stackRun.skycell_id,
+	    stackSumSkyfile.good_frac
+     FROM stackRun JOIN stackSumSkyfile USING(stack_id)
+     WHERE 
+     	   stackRun.state = 'full' AND
+	   stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
+	   -- template where hook %s
+     GROUP BY
+     	   skycell_id,
+	   filter
+) AS template ON stackRun.filter = template.filter 
+              AND stackRun.tess_id = template.tess_id 
+	      AND stackRun.skycell_id = template.skycell_id 
+LEFT JOIN (
+     SELECT 
+     	    diffRun.diff_id,
+	    stackRun.stack_id 
+     FROM diffRun JOIN diffInputSkyfile USING(diff_id) 
+     	  JOIN stackRun ON stackRun.stack_id = diffInputSkyfile.stack1 
+	  JOIN stackSumSkyfile USING(stack_id)
+	  WHERE 1
+	  -- diff where hook %s
+	  -- input where hook %s
+) AS diffExp ON stackRun.stack_id = diffExp.stack_id 
+WHERE
+	stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
+	-- input where hook again %s
+ORDER BY stackRun.data_group,stackRun.filter,stackRun.skycell_id
Index: trunk/ippTools/share/difftool_pendingcleanuprun.sql
===================================================================
--- trunk/ippTools/share/difftool_pendingcleanuprun.sql	(revision 26915)
+++ trunk/ippTools/share/difftool_pendingcleanuprun.sql	(revision 26927)
@@ -25,4 +25,5 @@
 	    USING(exp_id)
 	WHERE
+	@INNERCONSTRAINT@
 	    (diffRun.state = 'goto_cleaned' OR 
              diffRun.state = 'goto_scrubbed' OR 
@@ -52,4 +53,5 @@
 	    USING(exp_id)
 	WHERE
+	@INNERCONSTRAINT@
 	    (diffRun.state = 'goto_cleaned' OR 
              diffRun.state = 'goto_scrubbed' OR 
@@ -64,6 +66,6 @@
 	JOIN diffInputSkyfile
 	    USING(diff_id)
-	JOIN stackSumSkyfile
-	    ON  diffInputSkyfile.stack1 = stackSumSkyfile.stack_id
+--	JOIN stackSumSkyfile
+--	    ON  diffInputSkyfile.stack1 = stackSumSkyfile.stack_id
 	JOIN stackInputSkyfile
 	    ON diffInputSkyfile.stack1 = stackInputSkyfile.stack_id
@@ -79,4 +81,5 @@
 	    USING(exp_id)
 	WHERE
+	@INNERCONSTRAINT@
 	    (diffRun.state = 'goto_cleaned' OR 
              diffRun.state = 'goto_scrubbed' OR 
@@ -91,6 +94,6 @@
 	JOIN diffInputSkyfile
 	    USING(diff_id)
-	JOIN stackSumSkyfile
-	    ON  diffInputSkyfile.stack2 = stackSumSkyfile.stack_id
+--	JOIN stackSumSkyfile
+--	    ON  diffInputSkyfile.stack2 = stackSumSkyfile.stack_id
 	JOIN stackInputSkyfile
 	    ON diffInputSkyfile.stack2 = stackInputSkyfile.stack_id
@@ -106,4 +109,5 @@
 	    USING(exp_id)
 	WHERE
+	@INNERCONSTRAINT@
 	    (diffRun.state = 'goto_cleaned' OR 
              diffRun.state = 'goto_scrubbed' OR 
