oracle A级中至少有两个成绩单和至少有三个成绩单sql查询

enxuqcxy  于 2022-11-03  发布在  Oracle
关注(0)|答案(2)|浏览(198)

Consider two tables student(ssn, name, major) and transcript(ssn,grade) where ssn is primary key in student and foreign key in transcript. How do we find the students who received A in at least two transcripts with major CS. I have tried using having, group by to get the solution, but we shouldn't use group by and having. For three I have just replaced with the number at the end. Thank you for the answer in advance.

SELECT
  s.ssn,
  s.name,
  s.major
FROM student s
INNER JOIN (
  SELECT 
    ssn,
    COUNT(grade) as number_of_grades
  FROM transcript
  WHERE grade = 'A'
  GROUP BY ssn
) grade_count
ON grade_count.ssn = s.ssn

WHERE grade_count.number_of_grades >= 2
jaql4c8m

jaql4c8m1#

只有几个变体:
第一个
注:CBO可以决定去相关你的横向(外部应用)加入分组依据。见“横向视图去相关”转换。

ev7lccsx

ev7lccsx2#

您可以两次连接到脚本表:

SELECT s.ssn
FROM   student s
       INNER JOIN transcript t1
       ON (s.ssn = t1.ssn)
       INNER JOIN transcript t2
       ON (s.ssn = t2.ssn AND t1.ROWID <> t2.ROWID)
WHERE  s.major = 'CS'
AND    t1.grade = 'A'
AND    t2.grade = 'A'

相关问题