Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 26144)
+++ trunk/ippTools/share/Makefile.am	(revision 26147)
@@ -247,6 +247,5 @@
      stacktool_definebyquery_insert_random_part1.sql \
      stacktool_definebyquery_insert_random_part2.sql \
-     stacktool_definebyquery_part1.sql \
-     stacktool_definebyquery_part2.sql \
+     stacktool_definebyquery_select.sql \
      stacktool_definebyquery_test.sql \
      stacktool_donecleanup.sql \
Index: trunk/ippTools/share/stacktool_definebyquery_part1.sql
===================================================================
--- trunk/ippTools/share/stacktool_definebyquery_part1.sql	(revision 26144)
+++ 	(revision )
@@ -1,31 +1,0 @@
--- This is the first part of the query to get a list of skycells with
--- warps to be stacked, along with the number of warps already in a
--- stack.  It needs to be completed by part 2 (see below).
-
-SELECT
-    skycell_id,
-    filter,
-    tess_id,
-    num_warp,
-    MAX(num_stack) AS num_stack
-FROM ((
-    -- Number of stack-ready warps as a function of skycell and filter
-    SELECT
-        skycell_id,
-        warpSkyfile.tess_id as tess_id,
-        rawExp.filter,
-        COUNT(warpSkyfile.skycell_id) AS num_warp -- number of warps that can be stacked
-    FROM warpRun
-    JOIN warpSkyfile USING(warp_id)
-    JOIN fakeRun USING(fake_id)
-    JOIN camRun USING(cam_id)
-    JOIN camProcessedExp USING(cam_id)
-    JOIN chipRun USING(chip_id)
-    JOIN rawExp USING(exp_id)
-    WHERE
-        warpRun.state = 'full'
-        AND warpSkyfile.fault = 0
-        AND warpSkyfile.quality = 0
-    -- Here should follow the SQL in stacktool_definebyquery_part2.sql,
-    -- after any additional WHERE conditions have been added
-
Index: trunk/ippTools/share/stacktool_definebyquery_part2.sql
===================================================================
--- trunk/ippTools/share/stacktool_definebyquery_part2.sql	(revision 26144)
+++ 	(revision )
@@ -1,28 +1,0 @@
-
--- This is the second part of the query to get a list of skycells with
--- warps to be stacked, along with the number of warps already in a
--- stack.  It should follow part 1, which is in
--- stacktool_definebyquery_part1.sql
-
-    GROUP BY
-        skycell_id,
-        filter
-        ) AS warpsToStack
-        LEFT JOIN (
-    -- Number of stack inputs as a function of skycell and filter
-    SELECT
-        skycell_id,
-	stackRun.tess_id as stack_tess_id,
-        filter,
-        COUNT(stackInputSkyfile.warp_id) as num_stack -- number of warps in a stack
-    FROM stackRun
-        JOIN stackInputSkyfile USING(stack_id)
-    GROUP BY
-        stack_id
-        ) AS stackSizes
-    -- JOINing the warpsToStack and stackSizes tables
-        USING(skycell_id, filter)
-        )
-    GROUP BY
-        skycell_id,
-        filter
Index: trunk/ippTools/share/stacktool_definebyquery_select.sql
===================================================================
--- trunk/ippTools/share/stacktool_definebyquery_select.sql	(revision 26147)
+++ trunk/ippTools/share/stacktool_definebyquery_select.sql	(revision 26147)
@@ -0,0 +1,52 @@
+-- This is a query to get a list of skycells with warps to be stacked,
+-- along with the number of warps already in a stack.
+
+SELECT
+    skycell_id,
+    filter,
+    tess_id,
+    num_warp,
+    MAX(num_stack) AS num_stack
+FROM ((
+    -- Number of stack-ready warps as a function of skycell and filter
+    SELECT
+        skycell_id,
+        warpSkyfile.tess_id as tess_id,
+        rawExp.filter,
+        COUNT(warpSkyfile.skycell_id) AS num_warp -- number of warps that can be stacked
+    FROM warpRun
+    JOIN warpSkyfile USING(warp_id)
+    JOIN fakeRun USING(fake_id)
+    JOIN camRun USING(cam_id)
+    JOIN camProcessedExp USING(cam_id)
+    JOIN chipRun USING(chip_id)
+    JOIN rawExp USING(exp_id)
+    WHERE
+        warpRun.state = 'full'
+        AND warpSkyfile.fault = 0
+        AND warpSkyfile.quality = 0
+    -- WHERE hook %s
+    GROUP BY
+        skycell_id,
+        filter
+    ) AS warpsToStack
+LEFT JOIN (
+    -- Number of stack inputs as a function of skycell and filter
+    SELECT
+        skycell_id,
+        stackRun.tess_id as stack_tess_id,
+        filter,
+        COUNT(stackInputSkyfile.warp_id) as num_stack -- number of warps in a stack
+    FROM stackRun
+    JOIN stackInputSkyfile USING(stack_id)
+    -- WHERE hook %s
+    GROUP BY
+        stack_id
+    ) AS stackSizes
+-- JOINing the warpsToStack and stackSizes tables
+    USING(skycell_id, filter)
+    )
+GROUP BY
+    skycell_id,
+    filter
+
Index: trunk/ippTools/src/stacktool.c
===================================================================
--- trunk/ippTools/src/stacktool.c	(revision 26144)
+++ trunk/ippTools/src/stacktool.c	(revision 26147)
@@ -150,5 +150,5 @@
     PXOPT_COPY_F32(config->args,  where, "-select_iq_m4_min",          "camProcessedExp.iq_m4", ">=");
     PXOPT_COPY_F32(config->args,  where, "-select_iq_m4_max",          "camProcessedExp.iq_m4", "<=");
-    
+
     PXOPT_COPY_STR(config->args,  where, "-select_exp_type",           "rawExp.exp_type", "==");
     PXOPT_COPY_F32(config->args,  where, "-select_good_frac_min",      "warpSkyfile.good_frac", ">=");
@@ -173,5 +173,5 @@
     }
 
-    psString select = pxDataGet("stacktool_definebyquery_part1.sql");
+    psString select = pxDataGet("stacktool_definebyquery_select.sql");
     if (!select) {
         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
@@ -179,18 +179,16 @@
     }
 
+    psString where1 = psStringCopy("");
     if (psListLength(where->list)) {
         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
-        psStringAppend(&select, " AND %s", whereClause);
+        psStringAppend(&where1, "\nAND %s", whereClause);
         psFree(whereClause);
     }
-
-    psString groupby = pxDataGet("stacktool_definebyquery_part2.sql");
-    if (!groupby) {
-        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
-        psFree(where);
-        return false;
-    }
-    psStringAppend(&select, " %s", groupby);
-    psFree(groupby);
+    psFree(where);
+
+    psString where2 = psStringCopy("");
+    if (label) {
+        psStringAppend(&where2, "\nWHERE stackRun.label = '%s'", label);
+    }
 
     // Restriction on aggregated quantities using HAVING
@@ -224,11 +222,14 @@
     psFree(having);
 
-    if (!p_psDBRunQuery(config->dbh, select)) {
+    if (!p_psDBRunQueryF(config->dbh, select, where1, where2)) {
         psError(PS_ERR_UNKNOWN, false, "database error");
         psFree(select);
-        psFree(where);
+        psFree(where1);
+        psFree(where2);
         return false;
     }
     psFree(select);
+    psFree(where1);
+    psFree(where2);
 
     psArray *output = p_psDBFetchResult(config->dbh);
