set @fromparam= '2018-12-04';
set @toparam= DATE_ADD('2018-12-04', interval 86399 SECOND);
set @customerparam = 773;
set @daystartparam = SEC_TO_TIME(1-1);
set @startparam = DATE_ADD(@fromparam, interval 1 SECOND);
set @endparam = DATE_ADD(@toparam, interval (86399 + 1) SECOND);

set @taskparam = 1;
set @taskfilter=false;
set @taskdefault=false;

SELECT 
    P.*, COUNT(RI.roster_item_id) AS roster_count, IF(RI.task_id IS NULL, 0 , RI.task_id) AS task_id
FROM
    (SELECT 
        PC.cloud_date, PA.person_id
    FROM
        person PA
    CROSS JOIN cloud_date_table PC
    WHERE
        PA.customer_id = @customerparam
            AND PA.is_deleted = FALSE
            AND PC.cloud_date BETWEEN @fromparam AND @toparam
    LIMIT 0 , 10000) AS P
        LEFT JOIN
    user_group_membership UGM ON UGM.start_date <= P.cloud_date
        AND (UGM.end_date > P.cloud_date
        OR UGM.end_date IS NULL)
        AND UGM.member = P.person_id
        LEFT JOIN
    roster_item RI ON (RI.assigned_group = UGM.assigned_group_id
        OR RI.assigned_user = P.person_id)
        LEFT JOIN
    roster RO ON RI.roster_id = RO.roster_id
        AND RO.start_date <= P.cloud_date
        AND (RO.end_date IS NULL
        OR RO.end_date >= P.cloud_date)
        AND RI.day_number = DATEDIFF(P.cloud_date, RO.start_date) % 7 * RO.repetition
        AND RI.customer_id = @customerparam
GROUP BY P.cloud_date , P.person_id , RI.task_id
        
        
