sql to create output table