CREATE VIEW desks_data_replog AS
SELECT
    desks.desk_id AS desk_id,
    ifnull(desks_rep.countRep, 0) as countRep,
    desks_rep.avgRep
FROM
    desks
    LEFT JOIN (
        -- reps_data
        SELECT
            desks.desk_id,
            count(replog_data.desk_id) as countRep,
            avg(Grade) as avgRep
        FROM
            replog_data
            JOIN desks ON desks.user_id = replog_data.user_id
        WHERE
            replog_data.tree LIKE CONCAT('%', desks.desk_id, '%')
            and Grade < 4
        GROUP BY
            desks.desk_id
    ) desks_rep on desks.desk_id = desks_rep.desk_id