CREATE VIEW $viewName AS SELECT
    br.transaction_date,
    br.master_agent_id,
    op.role_tag AS operator_role_tag,
    ag.role_tag AS agent_role_tag,
    br.balance_category,
    SUM(br.bet_amount) AS turnover,
    SUM(br.win_amount) AS win,
    SUM(br.bet_amount - br.win_amount) AS ggr
FROM report_bets AS br
-- Operators
LEFT JOIN user_profiles AS opp ON opp.id = br.operator_id
LEFT JOIN users AS op ON op.id = opp.user_id
-- Master Agents
LEFT JOIN user_profiles AS map ON map.id = br.master_agent_id
LEFT JOIN users AS ma ON ma.id = map.user_id
-- Agents
LEFT JOIN user_profiles AS agp ON agp.id = br.agent_id
LEFT JOIN users AS ag ON ag.id = agp.user_id
GROUP BY br.transaction_date, br.balance_category, op.role_tag, ag.role_tag, br.master_agent_id