Here I am with a problem I can’t solve myself.
My problem is:
I need to SELECT the count of a column, but I also need to GROUP BY on that same column. What I’ve tried so far is not returning as I expected.
Here is what I tried:
'SELECT COUNT(user_id) AS total_donors
FROM ' . DONATION_SECURITY_TABLE .
" WHERE payment_status = 'Completed'
GROUP BY user_id"
This is how my table looks like:
id user_id payment_status
1 20 Completed
2 33 Completed
3 44 Completed
4 20 Pending
5 33 Pending
6 44 Completed
7 20 Completed
As you see, a single user_id can be Pending or Completed more than once, but I want my query to return 3 (based on the table example above).
So, I want my query to COUNT GROUPED user_ids if payment status is completed.
Ideas?
You can use the
DISTINCTkeyword to select the unique User ID’s like so