Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Executing two queries and exiting a unique table

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

+-------+------+-----+
| 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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading