-- +migrate Up
INSERT INTO job_number (account_id, job_number)
SELECT 
    a.id AS account_id,
    COALESCE(t.max_job_number, '00000000') AS job_number
FROM 
    accounts a
LEFT JOIN (
    SELECT 
        account_id, 
        MAX(job_number) AS max_job_number
    FROM 
        tasks
    GROUP BY 
        account_id
) t ON a.id = t.account_id;

-- +migrate Down
TRUNCATE TABLE job_number;