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

Count rows with specific data only, excluding data if it has another record with different details in another column

I have 3 columns in my table tbl_Result such as: id, Serial and Result, in Serial column data can be duplicate if it has pass and fail results, I want to select the Serial that has Pass only meaning don’t include the Serial with fail even it has a pass.

as you can see in my data Serial A1 and A5 has pass and fail results I don’t want to include that in my select query

I tried this code below but I’m still selecting the A1 and A5 Serial,
the total records I am expecting should be 8 only.

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 Serial, Result from tbl_Result where Result='pass' and Result not in('fail')
group by Serial, Result
having count(Serial)<2

Thanks in advance.

>Solution :

Looks like you need conditional aggregation. You are also grouping by too many columns.

SELECT
  Serial
FROM tbl_Result
GROUP BY
  Serial
HAVING COUNT(CASE WHEN Result = 'Fail' THEN 1 END) = 0;
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