SQL查找不同的对

时间:2014-02-16 15:29:56

标签: sql database relational-division

我正在试图弄清楚如何编写一个SQL查询来查找具有相同的季度,年份和类ID并且已经在多个classid中的不同学生ID对。

所以数据库看起来像这样

Studentid courseid quarter       year 
11035   1020       Fall          2012    
11035   1092       Fall          2012    
75234   3201       Winter        2012    
8871    1092       Fall          2013    
39077   1092       Fall          2013    
57923   9219       Winter        2013    
60973   9219       Winter        2013     
19992   3201       Winter        2013    
60973   8772       Spring        2013    
90421   8772       Spring        2013    
90421   2987       Spring        2013    
60973   2987       Spring        2013

我想要的结果是:

Studentid student id  year quarter course id

60973   90421   2013    Spring  8772    
60973   90421   2013    Spring  2987

到目前为止我已经

SELECT s.studentid, 
       st.studentid, 
       st.year, 
       s.quarter, 
       st.courseid
  FROM enrolled s 
       LEFT JOIN enrolled st 
                 ON st.COURSEID = s.COURSEID
 WHERE s.QUARTER = st.quarter
   AND s.YEAR = st.year 
   AND s.studentid <> st.studentid

但它给了我可以创造的每一个组合

3 个答案:

答案 0 :(得分:1)

自我加入两次 - 一次匹配,再次找到另一场比赛:

SELECT s.studentid, st.studentid, s.year, s.quarter, s.courseid
FROM enrolled s
JOIN enrolled st ON st.COURSEID = s.COURSEID
  AND st.quarter = s.quarter
  AND st.year = s.year
  AND s.studentid < st.studentid
JOIN enrolled e2 ON e2.COURSEID != s.COURSEID
  AND e2.quarter = s.quarter
  AND e2.year = s.year
  AND e2.studentid = st.studentid

输出:

| STUDENTID | OTHER_STUDENTID | YEAR | QUARTER | COURSEID |
|-----------|-----------------|------|---------|----------|
|     60973 |           90421 | 2013 |  Spring |     2987 |
|     60973 |           90421 | 2013 |  Spring |     8772 |

请参阅SQLFiddle

注意:

  • 您需要(内部)联接,而不是左联接
  • 使用s.studentid < st.studentid停止:
    • 学生加入自己,
    • 显示双方的双方
  • 为第二次加入使用不同的课程,但使用相同的其他学生ID

答案 1 :(得分:0)

SELECT a.*,b.courseid
FROM (SELECT   a.year
                     ,a.quarter
                     ,a.studentid AS Student1
                     ,b.studentid AS Student2
             FROM enrolled a
             JOIN enrolled b 
                 ON    a.COURSEID = b.COURSEID
                   AND a.QUARTER = b.quarter
                   AND a.YEAR = b.year 
                   AND a.courseid = b.courseid 
                   AND a.studentid < b.studentid
             GROUP BY a.year, 
                      a.quarter, 
                      a.studentid, 
                      b.studentid
             HAVING COUNT(*) > 1) a
JOIN enrolled b
  ON a.year = b.year
  AND a.quarter = b.quarter
  AND a.Student1 = b.studentid

SQL Fiddle

我在表格中添加了一行:(11035, 2987, 'Spring', 2013)该行打破了一些与您的示例数据相关的答案。您需要一个HAVING条款,以确保这对学生在特定的一个季度中共有一个以上的课程。

答案 2 :(得分:0)

使用CTE作为聚合:

WITH pairs AS (
        SELECT distinct e1.Studentid AS student1
        , e2.Studentid AS student2
        FROM enrolled e1
        JOIN enrolled e2 ON e1.courseid = e2.courseid AND e1.quarter = e2.quarter AND e1.year = e2.year
        WHERE e1.Studentid < e2.Studentid
        GROUP BY e1.Studentid,e2.Studentid
        HAVING COUNT(*) > 1
        )
SELECT e1.Studentid AS student1
        , e2.Studentid AS student2
        , e1.courseid
        , e1.quarter
        , e1.year
FROM pairs p
JOIN enrolled e1 ON e1.Studentid = p.student1
JOIN enrolled e2 ON e2.Studentid = p.student2
WHERE e1.courseid = e2.courseid AND e1.quarter = e2.quarter AND e1.year = e2.year
        ;