CREATE VIEW cards_data_replog AS
SELECT
    cards.card_id AS card_id,
    cards.desk_id AS desk_id,
    cards.last_timing AS last_timing,
    cards.next_timing AS next_timing,
    cards.level AS level,
    ifnull(cards_rep.countRep, 0) as countRep,
    avgRep AS avgRep
FROM
    cards
    LEFT JOIN (
        -- reps_data
        SELECT
            replog.card_id,
            count(replog.card_id) as countRep,
            avg(Grade) as avgRep
        FROM
            replog
        where
            Grade < 4
        GROUP BY
            replog.card_id
    ) cards_rep on cards.card_id = cards_rep.card_id