Index: trunk/ippTools/share/difftool_definestackstack_part0.sql
===================================================================
--- trunk/ippTools/share/difftool_definestackstack_part0.sql	(revision 26956)
+++ trunk/ippTools/share/difftool_definestackstack_part0.sql	(revision 26958)
@@ -5,4 +5,8 @@
        stackRun.label AS INPUT_label,
        stackRun.tess_id AS INPUT_tess_id
+--     ,
+--       stackRun.stack_id AS INPUT_stack_id,
+--       template.max_stack_id,
+--       diffExp.diff_id
 FROM stackRun JOIN stackSumSkyfile USING(stack_id) 
 JOIN (
@@ -28,14 +32,15 @@
      SELECT 
      	    diffRun.diff_id,
-	    stackRun.stack_id 
+	    diffInputSkyfile.stack1,
+	    diffInputSkyfile.stack2
      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 
+) AS diffExp ON diffExp.stack1 = stackRun.stack_id AND diffExp.stack2 = template.max_stack_id
 WHERE
 	stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
+        -- diff where hook %s
 	-- 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 26956)
+++ trunk/ippTools/share/difftool_definestackstack_part1.sql	(revision 26958)
@@ -33,14 +33,15 @@
      SELECT 
      	    diffRun.diff_id,
-	    stackRun.stack_id 
+	    diffInputSkyfile.stack1,
+	    diffInputSkyfile.stack2
      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 
+) AS diffExp ON diffExp.stack1 = stackRun.stack_id AND diffExp.stack2 = template.max_stack_id
 WHERE
 	stackSumSkyfile.fault = 0 AND stackSumSkyfile.quality = 0
+	-- diff where hook %s
 	-- input where hook again %s
 ORDER BY stackRun.data_group,stackRun.filter,stackRun.skycell_id
Index: trunk/ippTools/src/difftool.c
===================================================================
--- trunk/ippTools/src/difftool.c	(revision 26956)
+++ trunk/ippTools/src/difftool.c	(revision 26958)
@@ -1894,5 +1894,5 @@
   psString diffQuery = NULL;
   if (! (reRun || newTemplates) ) {
-    psStringAppend(&diffQuery, "\n AND diff_id IS NULL");
+    psStringAppend(&diffQuery, "\n AND diffExp.diff_id IS NULL");
   } else {
     diffQuery = psStringCopy("\n");
@@ -1905,5 +1905,7 @@
     return(false);
   }
-
+  psTrace("difftool",1,query,stack2Query,diffQuery,stack1Query,stack1Query);
+/*   exit(10); */
+	  
   if (!psDBTransaction(config->dbh)) {
     psError(PS_ERR_UNKNOWN, false, "database error");
@@ -1911,5 +1913,5 @@
   }
 
-  if (!p_psDBRunQueryF(config->dbh, query, stack2Query, diffQuery, stack1Query, stack1Query)) {
+  if (!p_psDBRunQueryF(config->dbh, query, stack2Query, stack1Query, diffQuery, stack1Query)) {
     psError(PS_ERR_UNKNOWN, false, "database error");
     psFree(query);
@@ -1919,4 +1921,5 @@
     return false;
   }
+
   psFree(query);
 
@@ -1978,5 +1981,5 @@
     psFree(thisWhere);
 
-/*     psTrace("difftool",1, query,stack2Query,diffQuery,this_stack1Query,this_stack1Query); */
+    psTrace("difftool",1, query,stack2Query,diffQuery,this_stack1Query,this_stack1Query);
     if (!psDBTransaction(config->dbh)) {
       psError(PS_ERR_UNKNOWN, false, "database error");
@@ -1984,5 +1987,5 @@
     }
 
-    if (!p_psDBRunQueryF(config->dbh, query, stack2Query, diffQuery, this_stack1Query, this_stack1Query)) {
+    if (!p_psDBRunQueryF(config->dbh, query, stack2Query, this_stack1Query, diffQuery, this_stack1Query)) {
       psError(PS_ERR_UNKNOWN, false, "database error");
       psFree(query);
