i have a table called users which has users and respective referralIds. Some of the users do not have referralIds and are NULL.
I have a second table called transactions where sum(transactions.amount) will give me the revenue that the customer generated as long as transaction_type = 1
Problem: I am doing a left join that will show me revenue generated per referralId. I want a group by that shows me revenue generated by users that do not have referralIds a well. However, Null referralId is returning NULL revenue as well
SELECT
users.referralId, b.revenue
FROM users
LEFT JOIN
(SELECT
users.referralId, SUM(transactions.amount) AS revenue
FROM
transactions
LEFT JOIN users ON users.username = transactions.username
WHERE
transaction_type = 1 and date(created_at) between @start_date and @end_date
GROUP BY users.referralId) b ON users.referralId = b.referralId
group by referralId