2017-07-17 68 views
0

如何組合兩者以顯示學生人數超過6的班級的學生姓名(sname)和部門編號(sectno)?到目前爲止,我有這個在SQL中合併頻率計數和INNER CASE

SELECT student.sname, enroll.sectno, 

FROM student 

INNER JOIN enroll 

ON student.sid=enroll.sid 

SELECT grade, 

COUNT(grade)AS Frequency 

FROM enroll 

GROUP BY grade 

HAVING COUNT(grade)>6 
+0

什麼是'student'和'enroll'表之間的關係? –

回答

0

看起來你是非常接近的。我認爲以下應爲你工作:

SELECT 
student.sname, 
enroll.sectno, 
COUNT(enroll.grade) AS Frequency 
FROM student 
INNER JOIN enroll ON student.sid=enroll.sid 
GROUP BY student.sname, enroll.sectno 
HAVING COUNT(enroll.grade)>6 
+0

我找不到應該在哪裏聲明我的COUNT變量。我在聲明我的INNER JOIN後試過使用WHERE子句。感謝您的時間和幫助。這是非常翔實的 –

+0

感謝您的反饋@ S.Rain樂意幫助! :) – WJS

0

在你爲2Md選擇沒有關係有超過6學生,恕我直言GROUP BY應根據柱像classid類。然後你就可以使用窗總結簡單地結合兩種:

with cte as 
(
    SELECT student.sname, enroll.sectno, 
     -- get the count per class, might be a different column than sectno 
     count(*) over (partition by enroll.sectno) as cnt 
    FROM student 
    INNER JOIN enroll 
    ON student.sid=enroll.sid 
) 
select * from cte 
where cnt > 6 
+0

我明白了。我對SQL本身很陌生,所以我從來沒有想過要使用Partition By或使用Windowed Aggregate。這非常有幫助 –