Skip to content
Advertisement

How to count two different columns in sql

I have a database which looks like this.

My task is to get the columns of dates, number of exams on that date, number of students who write an exam on that date. Something like Date – Exam Count – Student Count. I have done this but in two different queries.

Result: edate Exams | ==================| 2020-05-24 | 1 | 2020-05-23 | 1 | 2020-05-22 | 2 | 2020-05-20 | 1 | 2020-05-19 | 1 | 2020-05-18 | 1 | 2020-05-16 | 2 | 2020-05-15 | 2 | 2020-05-14 | 5 | ==================|

Result: edate Writing | ====================| 2020-05-24 | 23 | 2020-05-23 | 27 | 2020-05-22 | 40 | 2020-05-20 | 30 | 2020-05-19 | 27 | 2020-05-18 | 25 | 2020-05-16 | 52 | 2020-05-15 | 34 | 2020-05-14 | 116 | ====================|

After I tried to combine these two queries into one and I got this, but it gives me the same count at every row, see the image below:

enter image description here

I know that’s because of adding new table, but how do I make it work?

Advertisement

Answer

You’ve almost got it. Just combine your two queries:

NOTE: Not tested, no MySQL on current box

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement