2014-02-26 28 views
1

我有一張名爲招生的表。學生A有3條記錄,其中兩條具有相同的start_date。如何比較postgres中的兩個字段?

我想在postgres中找到所有2013年具有相同開始日期的學生。

報名:

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

在此先感謝。

回答

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

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 
)