Skip to content
Advertisement

SQL query to filter records based on count and status

I have to filter records based on the status when the count is more than 1. Column names: Student_id, Status, term, and course.

DB: Postgres

Condition to filter:

  1. If there exists only one record for the student then the status(true or false) does not matter. Fetch the record.
  2. If record count for a student is more than one then fetch only those students whose status is true. (More than one record would mean, same Student_id, term, and course). At any given time there will be only one record with status as true.

How do I write the SQL query for this?

Advertisement

Answer

You can try below query:

Result will be like below:

id Student_id Status term course

1 1 1 3 C#

4 2 1 6 Php

5 2 1 7 Php

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement