Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 19219)
+++ trunk/ippTools/share/Makefile.am	(revision 19249)
@@ -132,5 +132,6 @@
      regtool_revertprocessedimfile.sql \
      stacktool_definebyquery_insert.sql \
-     stacktool_definebyquery_insert_random.sql \
+     stacktool_definebyquery_insert_random_part1.sql \
+     stacktool_definebyquery_insert_random_part2.sql \
      stacktool_definebyquery_part1.sql \
      stacktool_definebyquery_part2.sql \
Index: trunk/ippTools/share/stacktool_definebyquery_insert_random.sql
===================================================================
--- trunk/ippTools/share/stacktool_definebyquery_insert_random.sql	(revision 19219)
+++ 	(revision )
@@ -1,33 +1,0 @@
--- Use a random set of warps for the inputs to a defined stack
-INSERT INTO
-        stackInputSkyfile(stack_id, warp_id)
-SELECT
-        @STACK_ID@, -- This should be replaced with the stack_id
-        warp_id
-FROM (
-    -- Sub-select to get the random set of warps
-    SELECT
-            warpSkyfile.*,
-            rand() AS rnd_num
-    FROM warpSkyfile
-    JOIN warpRun
-        USING(warp_id, tess_id)
-    JOIN fakeRun
-        USING(fake_id, tess_id)
-    JOIN camRun
-        USING(cam_id, tess_id)
-    JOIN chipRun
-        USING(chip_id, tess_id)
-    JOIN rawExp
-        USING(exp_id, tess_id)
-    WHERE
-        skycell_id = '%s'
-        AND tess_id = '%s'
-        AND rawExp.filter = '%s'
-        AND warpSkyfile.fault = 0
-        AND warpSkyfile.ignored = 0
-    -- Sort by the random number, and take the first N
-    -- to get a random set of N.
-    ORDER BY rnd_num
-    LIMIT %d
-) AS randomWarps
Index: trunk/ippTools/share/stacktool_definebyquery_insert_random_part1.sql
===================================================================
--- trunk/ippTools/share/stacktool_definebyquery_insert_random_part1.sql	(revision 19249)
+++ trunk/ippTools/share/stacktool_definebyquery_insert_random_part1.sql	(revision 19249)
@@ -0,0 +1,32 @@
+-- This is the part 1 of 2 of a query to use a random set of warps for
+-- the inputs to a defined stack.
+-- stacktool_definebyquery_insert_random_part2.sql should be appended.
+INSERT INTO
+        stackInputSkyfile(stack_id, warp_id)
+SELECT
+        @STACK_ID@, -- This should be replaced with the stack_id
+        warp_id
+FROM (
+    -- Sub-select to get the random set of warps
+    SELECT
+            warpSkyfile.*,
+            rand() AS rnd_num
+    FROM warpSkyfile
+    JOIN warpRun
+        USING(warp_id, tess_id)
+    JOIN fakeRun
+        USING(fake_id, tess_id)
+    JOIN camRun
+        USING(cam_id, tess_id)
+    JOIN chipRun
+        USING(chip_id, tess_id)
+    JOIN rawExp
+        USING(exp_id, tess_id)
+    WHERE
+        skycell_id = '%s'
+        AND tess_id = '%s'
+        AND rawExp.filter = '%s'
+        AND warpSkyfile.fault = 0
+        AND warpSkyfile.ignored = 0
+-- Put additional constraints here
+-- stacktool_definebyquery_insert_random_part2.sql goes here
Index: trunk/ippTools/share/stacktool_definebyquery_insert_random_part2.sql
===================================================================
--- trunk/ippTools/share/stacktool_definebyquery_insert_random_part2.sql	(revision 19249)
+++ trunk/ippTools/share/stacktool_definebyquery_insert_random_part2.sql	(revision 19249)
@@ -0,0 +1,8 @@
+-- This is the part 2 of 2 of a query to use a random set of warps for
+-- the inputs to a defined stack.
+-- stacktool_definebyquery_insert_random_part1.sql should be prepended.
+    -- Sort by the random number, and take the first N
+    -- to get a random set of N.
+    ORDER BY rnd_num
+    LIMIT %d
+) AS randomWarps
