I have a database, and I would like to get data where the username is equal to x and I would also like to get data regardless of the user.
I have the two queries:
SELECT MONTH(date) as month, COUNT(*) as count
from DB_NAME
WHERE YEAR(date) = YEAR(CURDATE()) AND userid = 'userid'
GROUP BY month
SELECT MONTH(date) as month, COUNT(*) as count
from DB_NAME
WHERE YEAR(date) = YEAR(CURDATE())
GROUP BY month
Expected output:
+-------+------+-----+
| month | user | all |
+-------+------+-----+
| 1 | 100 | 200 |
| 2 | 90 | 150 |
+-------+------+-----+
>Solution :
I think some conditional aggregation is what you are after here:
SELECT
MONTH(date) as month,
COUNT(CASE WHEN userid = 'userid' THEN 1 END) as user,
COUNT(*) as count
from DB_NAME
WHERE YEAR(date) = YEAR(CURDATE())
GROUP BY month;