SELECT parthner.parthner_id, (
select count(parthner_referal.user_id) from parthner_referal where parthner_referal.parthner_id = parthner.parthner_id
) as count,
count(payment.amount) as amount,
user.name, user.external_uid
FROM parthner
left JOIN user ON parthner.user_id = user.user_id
LEFT JOIN parthner_referal ON parthner.parthner_id = parthner_referal.parthner_id
LEFT JOIN payment ON parthner_referal.user_id = payment.user_id AND status = 1
WHERE parthner_referal.created_at BETWEEN '2022-10-01 22:26:02' AND '2023-11-13 22:26:02'
group by parthner_id
order by count + amount desc