Index: trunk/ippTools/share/Makefile.am
===================================================================
--- trunk/ippTools/share/Makefile.am	(revision 28941)
+++ trunk/ippTools/share/Makefile.am	(revision 28959)
@@ -153,4 +153,5 @@
 	disttool_definebyquery_fake.sql \
 	disttool_definebyquery_raw.sql \
+	disttool_definebyquery_sky.sql \
 	disttool_definebyquery_stack.sql \
 	disttool_definebyquery_warp.sql \
@@ -166,4 +167,5 @@
 	disttool_pending_fake.sql \
 	disttool_pending_raw.sql \
+	disttool_pending_sky.sql \
 	disttool_pending_stack.sql \
 	disttool_pending_warp.sql \
@@ -328,4 +330,7 @@
 	staticskytool_definebyquery_select.sql \
 	staticskytool_definebyquery_inputs.sql \
+	staticskytool_export_run.sql \
+	staticskytool_export_input.sql \
+	staticskytool_export_result.sql \
 	staticskytool_inputs.sql \
 	staticskytool_todo.sql \
Index: trunk/ippTools/share/disttool_definebyquery_sky.sql
===================================================================
--- trunk/ippTools/share/disttool_definebyquery_sky.sql	(revision 28959)
+++ trunk/ippTools/share/disttool_definebyquery_sky.sql	(revision 28959)
@@ -0,0 +1,27 @@
+SELECT DISTINCT
+    'sky' as stage,
+    staticskyRun.sky_id AS stage_id,
+    CAST(0 AS SIGNED) AS magicked,
+    -- run tag in the form 'sky.$skycell_id.$stack_id'
+    CONCAT_WS('.', 'sky', stackRun.skycell_id, convert(staticskyRun.sky_id, CHAR)) as run_tag,
+    staticskyRun.label,
+    staticskyRun.data_group,
+    distTarget.dist_group,
+    distTarget.target_id,
+    distTarget.clean
+FROM staticskyRun
+JOIN staticskyResult USING(sky_id)
+JOIN staticskyInput USING(sky_id)
+JOIN stackRun using(stack_id)
+JOIN distTarget ON distTarget.stage = 'sky'
+    AND staticskyRun.dist_group = distTarget.dist_group
+JOIN rcInterest USING(target_id)
+LEFT JOIN distRun ON (distRun.stage_id = sky_id)
+    AND distRun.target_id = distTarget.target_id
+    -- JOIN hook %s
+WHERE  distTarget.state = 'enabled'
+    AND rcInterest.state = 'enabled'
+    AND distRun.dist_id IS NULL
+    AND ((staticskyRun.state = 'full') OR (distTarget.clean AND staticskyRun.state = 'cleaned'))
+    -- we shouldn't need to check fault. If faulted it shouldn't be full
+    AND (staticskyResult.fault = 0 AND staticskyResult.quality = 0)
Index: trunk/ippTools/share/disttool_pending_sky.sql
===================================================================
--- trunk/ippTools/share/disttool_pending_sky.sql	(revision 28959)
+++ trunk/ippTools/share/disttool_pending_sky.sql	(revision 28959)
@@ -0,0 +1,50 @@
+SELECT DISTINCT
+    distRun.dist_id,
+    distRun.label,
+    distTarget.dist_group,
+    stage,
+    stage_id,
+    stackRun.skycell_id AS component,
+    clean,
+    rawExp.camera,
+    CONCAT_WS('.', outroot, CONVERT(distRun.dist_id, CHAR)) as outdir,
+    staticskyResult.path_base,
+    CAST(NULL AS CHAR(255)) as chip_path_base,
+    staticskyRun.state,
+    staticskyRun.state AS data_state,
+    staticskyResult.quality,
+    1 AS no_magic,
+    0 AS magicked
+FROM distRun
+JOIN distTarget USING(target_id, stage, clean)
+JOIN staticskyRun
+    ON stage_id = sky_id
+JOIN staticskyResult
+    USING(sky_id)
+JOIN staticskyInput
+    USING(sky_id)
+JOIN stackRun USING(stack_id)
+JOIN stackInputSkyfile USING(stack_id)
+JOIN warpSkyfile
+    ON  stackInputSkyfile.warp_id = warpSkyfile.warp_id
+    AND stackRun.skycell_id       = warpSkyfile.skycell_id
+    AND stackRun.tess_id          = warpSkyfile.tess_id
+JOIN warpRun
+    ON warpRun.warp_id = warpSkyfile.warp_id
+JOIN fakeRun
+    USING(fake_id)
+JOIN camRun
+    USING(cam_id)
+JOIN chipRun
+    ON camRun.chip_id = chipRun.chip_id
+JOIN rawExp 
+     USING (exp_id)
+LEFT JOIN distComponent 
+    ON distRun.dist_id = distComponent.dist_id 
+    AND stackRun.skycell_id = distComponent.component
+WHERE
+    distRun.state = 'new'
+    AND distRun.stage = 'sky'
+    AND distComponent.dist_id IS NULL
+    AND (staticskyRun.state = 'full' OR (distRun.clean AND staticskyRun.state = 'cleaned'))
+    AND (staticskyResult.fault = 0 AND staticskyResult.quality = 0)
Index: trunk/ippTools/share/disttool_toadvance.sql
===================================================================
--- trunk/ippTools/share/disttool_toadvance.sql	(revision 28941)
+++ trunk/ippTools/share/disttool_toadvance.sql	(revision 28959)
@@ -240,4 +240,25 @@
         AND distComponent.fault = 0
 UNION
+-- staticsky stage
+-- NOTE this assumes that there is only one component per staticskyRun
+-- (one skycell)
+SELECT
+    distRun.dist_id,
+    stage,
+    stage_id,
+    outroot,
+    label,
+    clean
+    FROM distRun
+    JOIN staticskyResult on stage_id = sky_id
+    LEFT JOIN distComponent
+        ON distRun.dist_id = distComponent.dist_id
+    WHERE
+        distRun.state = 'new'
+        AND distRun.fault = 0
+        AND distRun.stage = 'sky'
+        AND distComponent.component IS NOT NULL
+        AND distComponent.fault = 0
+UNION
 -- SSdiff stage
 SELECT
Index: trunk/ippTools/share/staticskytool_export_input.sql
===================================================================
--- trunk/ippTools/share/staticskytool_export_input.sql	(revision 28959)
+++ trunk/ippTools/share/staticskytool_export_input.sql	(revision 28959)
@@ -0,0 +1,1 @@
+SELECT staticskyInput.* FROM staticskyInput
Index: trunk/ippTools/share/staticskytool_export_result.sql
===================================================================
--- trunk/ippTools/share/staticskytool_export_result.sql	(revision 28959)
+++ trunk/ippTools/share/staticskytool_export_result.sql	(revision 28959)
@@ -0,0 +1,1 @@
+SELECT staticskyResult.* FROM staticskyResult
Index: trunk/ippTools/share/staticskytool_export_run.sql
===================================================================
--- trunk/ippTools/share/staticskytool_export_run.sql	(revision 28959)
+++ trunk/ippTools/share/staticskytool_export_run.sql	(revision 28959)
@@ -0,0 +1,1 @@
+SELECT staticskyRun.* from staticskyRun
Index: trunk/ippTools/share/staticskytool_result.sql
===================================================================
--- trunk/ippTools/share/staticskytool_result.sql	(revision 28941)
+++ trunk/ippTools/share/staticskytool_result.sql	(revision 28959)
@@ -1,11 +1,12 @@
-SELECT
+SELECT DISTINCT
     staticskyResult.*,
     staticskyRun.state,
-    staticskyRun.tess_id,
-    staticskyRun.skycell_id,
-    staticskyRun.filter,
     staticskyRun.workdir,
     staticskyRun.label,
+    stackRun.tess_id,
+    stackRun.skycell_id,
+    stackRun.filter
 FROM staticskyRun
-JOIN staticskyResult
-USING(stack_id)
+JOIN staticskyResult USING(sky_id)
+JOIN staticskyInput USING(sky_id)
+JOIN stackRun using(stack_id)
