Skip to content
Advertisement

SQL Statement: Group BY not working as it is needed to

I am trying to get a count of the number of students that are absent at a particular day. I am going to be providing a sample of the view here:

enter image description here

I am trying to get a count a view that shows me School_Name, Absent_Date, Name and Student_ID from the data above where a student has skipped more than or equal to 4 periods.

My SQL statement is like the following:

SELECT [PERIOD], [Student_ID], [ABS_DATE]  
FROM t_database
GROUP BY [Bell_Period]  
HAVING COUNT(*) >= 4

However, it does not show it, I have not worked with a scenario such as this thus, any help would be appreciated.

Advertisement

Answer

You need to group by the columns you want in the result, and aggregate (here – count) across those you don’t:

SELECT   school_name, absent_date, name, student_id
FROM     t_database
GROUP BY school_name, absent_date, name, student_id
HAVING   COUNT(*) >= 4
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement