I have a usecase on my studies for below example,
i am struggling with the logic on how i can do a select statement to get all the records to display a student who failed in one or more subject
Assuming i have below table
Student_id | name | subject | marks |
---|---|---|---|
s1 | adam | english | 35 |
s1 | adam | math | 69 |
s1 | adam | science | 55 |
s2 | bob | english | 56 |
s2 | bob | math | 76 |
s2 | bob | science | 88 |
s3 | pop | english | 33 |
s3 | pop | math | 90 |
s3 | pop | science | 76 |
Expected output should be
Student_id | name | subject | marks |
---|---|---|---|
s1 | adam | english | 35 |
s1 | adam | math | 69 |
s1 | adam | science | 55 |
s3 | pop | english | 33 |
s3 | pop | math | 90 |
s3 | pop | science | 76 |
I have tried with this below query but it displaying none value
select count(*)
from (select student_id
from student
where marks < 50
group by student_id
having count(subject) > 1) as dt
3
Answers
This query will first select all records from the marks table where the marks are less than 40. It will then group the records by Student_id and count the number of records in each group
You can do it using
SUM OVER()
clause as follows :This does the group by Student_id. And it calculates the number of failed object using
SUM CASE
clauseDemo here
I think something like this would work and is pretty simple: