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.*,  IF(E.event_count IS NULL , 0 , E.event_count) AS event_count, IF(E.task_id IS NULL, 0, E.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
    (SELECT 
        COUNT(E.event_id) AS event_count,
            P.person_id,
            E.task_id,
            DATE(CASE
                WHEN TIME(E.shift_start) >= @daystartparam THEN E.shift_start
                ELSE DATE_SUB(E.shift_start, INTERVAL 1 DAY)
            END) AS event_date
    FROM
        eventlog E
    INNER JOIN person P ON E.person_id = P.person_id
        AND P.customer_id = @customerparam
        AND E.is_deleted = FALSE
        AND E.shift_start BETWEEN @fromparam AND @toparam
        AND (NOT @taskfilter
        OR ((NOT @taskdefault
        AND E.task_id = @taskparam)
        OR (@taskdefault AND E.task_id IS NULL)))
    GROUP BY E.shift_start , E.person_id , E.task_id) AS E ON P.person_id = E.person_id
        AND P.cloud_date = E.event_date 
        
        
