user2926497
user2926497

Reputation: 525

How to compare two fields in postgres?

I have a table called enrollment. Student A has 3 records, with two of them have the same start_date.

I would like to find all the students with the same start date for the year 2013 in postgres.

Enrollment :

StudentID   Start_Date      Syear school_id
1             2013-06-21    2013    10
1             2013-06-21    2013    11 
1             2014-02-21    2014    10 

Thanks in advance.

Upvotes: 1

Views: 198

Answers (1)

Daniel Sparing
Daniel Sparing

Reputation: 2173

SELECT StudentID, Start_Date
FROM
(
    SELECT StudentID, Start_date, COUNT(*) OVER (PARTITION BY start_date) count
    FROM Student
)
WHERE count > 1

or

SELECT StudentID, Start_Date
FROM Student S1
WHERE EXISTS (
    SELECT *
    FROM Student S2
    WHERE S1.Start_Date = S2.Start_Date AND S1.StudentID <> S2.StudentID
)

Upvotes: 1

Related Questions