I have following table structure:
Student School Code Roll Number A 12 135 B 12 248 C 16 934 D 16 437 E 12 249
I want to get the count of students in one school code and display the count. I can do a row_number and get the number of rows in this way:
select student, School code, row_number() over(partition by School Code order by Roll Number) AS line_no from table ;
the output would be:
Student School Code line_no Total_Count A 12 1 3 B 12 2 3 E 12 3 3 D 16 1 2 C 16 2 2
I am not able to write the sql code to produce the last column (i.e Total_Count). Can you please tell me how to do it.
Advertisement
Answer
You want the count()
window function:
select student, School code, row_number() over(partition by School Code order by Roll Number) as line_no count(*) over(partition by School Code) as total_count from table ;