SELECT 
    task_task.*,
    users_profile.name AS account_owner_name,
    users_profile.profile_image AS account_owner_avatar,
    CASE 
        WHEN task_task.volume = 0 THEN task_task.progress_percentage 
        ELSE (task_task.volume_current * 100.0 / task_task.volume) 
    END AS volume_percent,
	COALESCE(task_task.volume * 1.0 / COALESCE(bobot_volume.bobot_volume, 0), 0) AS bobot_volume,
    COALESCE((1 * 1.0 / COALESCE(bobot_novolume.bobot_novolume, 0)), 0) AS bobot_novolume,
    COALESCE((task_task.volume_current * 100.0 / task_task.volume) * COALESCE(task_task.volume * 1.0 / COALESCE(bobot_volume.bobot_volume, 0), 0), 0) as percentage_volume,
    COALESCE(task_task.progress_percentage * COALESCE((1 * 1.0 / COALESCE(bobot_novolume.bobot_novolume, 0)), 0), 0) as percentage_novolume
FROM task_task
JOIN users_profile ON task_task.account_owner_id = users_profile.id
LEFT JOIN (
    SELECT 
        account_owner_id,
        COUNT(id) AS bobot_novolume
    FROM task_task
    WHERE is_volume = 0
    GROUP BY account_owner_id
) AS bobot_novolume ON bobot_novolume.account_owner_id = task_task.account_owner_id
LEFT JOIN (
    SELECT 
        account_owner_id,
        SUM(volume) AS bobot_volume
    FROM task_task
    WHERE is_volume = 1
    GROUP BY account_owner_id
) AS bobot_volume ON bobot_volume.account_owner_id = task_task.account_owner_id
ORDER BY task_task.id;


SELECT 
        task_task.*,
        users_profile.name AS account_owner_name,
        users_profile.profile_image AS account_owner_avatar,
        CASE 
            WHEN volume = 0 THEN task_task.progress_percentage 
            ELSE (volume_current * 100.0 / volume) 
        END AS progress_percent,
        CASE 
            WHEN volume = 0 THEN 0 
            ELSE (volume * (volume_current * 1.0 / volume) * 100 / (SELECT SUM(volume) FROM task_task WHERE account_owner_id = task_task.account_owner_id)) 
        END AS all_percentage
    FROM task_task 
    JOIN users_profile ON task_task.account_owner_id = users_profile.id
    ORDER BY task_task.id;