Reputation: 39
As there are 3 different results for Student_ID 111. I need a query to fetch the row which has the result as 'pass' as well fetch the row with student_ID 112 which as a result as Fail.
Thanks for your help!
Upvotes: 0
Views: 590
Reputation:
You can try this..
SELECT * FROM `tblname` WHERE Result = 'Pass' OR Result = 'Fail'
Upvotes: 0
Reputation: 154
I would do it with row_number and partition by
create table #t(student_id int, name varchar(50), result varchar(50))
insert into #t values
(111,'Jhon','Pass'),
(111,'Jhon','Absent'),
(111,'Jhon','Fail'),
(112,'Max','Fail');
WITH filterQuery as(
select
ROW_NUMBER() OVER(PARTITION BY student_id ORDER BY CASE result
WHEN 'Pass' THEN 0
WHEN 'Fail' THEN 1
WHEN 'Absent' THEN 2
END ASC)
AS Row ,
* from #t
)
select student_id, name, result from filterQuery where Row = 1
DROP Table #t
But the database design is not really good
Upvotes: 1
Reputation: 416
Can you please try below Query:
select * from Student where Result = 'Pass' Or (Student_ID = 112 And Result = 'Fail')
Upvotes: 0