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

can i can use two group by in sql query

Hi i have two table

  Covids
    | CovidId | StaffId | CovidStartDate | CovidFinishDate | 
      -------   -------   --------------   ---------------
        1           1       21.01.2021         15.01.2021
        2           1       26.04.2021         16.04.2021
        3           2       21.02.2021         14.01.2021
        4           3       22.03.2021         17.03.2021
        5           3       15.04.2021         30.04.2021
  
   WorkingHours
    | WorkId | StaffId | WorkHours| 
     -------   -------   ---------
        1          1          8
        2          2          9
        3          3          8

I want to show how many times they had covid by working hour
expected result

    | WorkHour | CovidCount| 
     -------      -------   
        8           4          
        9           1          
             

i wrote this but i didn’t unite

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

Select StaffId,Count(*) as covidCount From Covids group by StaffId
Select WorkHours From WorkingHours group by WorkHours

>Solution :

Of course two separate queries won’t "unite". You need to join the tables and then do the aggregation.

SELECT w.workhours AS workhour,
       count(*) AS covidcount
       FROM workinghours AS w
            LEFT JOIN covids AS c
                      ON c.staffid = w.staffid
       GROUP BY w.workhours;
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