
-- 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
