2012-04-20 81 views
-2

我有包括以下SQL和計數

Student Name  Grade  Class 
-------------------------------------------------------- 
User 1    A   English 
User 1    B   Math 
User 2    B   Math 

我試圖創建一個查詢,它會列出所有學生的表格,總等級(等級A,B & C) ,總數A,總數B,總數C

有人可以幫助我創建查詢或至少讓我在正確的方向嗎?

+0

什麼是您的數據庫? – dasblinkenlight 2012-04-20 04:02:42

+0

您是否只查看每個年級的總計? – 2012-04-20 04:06:37

+0

@dasblinkenlight:MS Access – Imran 2012-04-20 04:15:27

回答

3

試試這個:

select name, 
    count(case when grade in ('A', 'B', 'C') then 1 end) totalPass, 
    count(case when grade = 'A' then 1 end) totalA, 
    count(case when grade = 'B' then 1 end) totalB, 
    count(case when grade = 'C' then 1 end) totalC 
from t 
group by name 

這裏是fiddle

或者我們也可以,如果你使用MySQL使它更簡單:

select name, 
    sum(grade in ('A', 'B', 'C')) totalPass, 
    sum(grade = 'A') totalA, 
    sum(grade = 'B') totalB, 
    sum(grade = 'C') totalC 
from t 
group by name 

這裏是fiddle

+1

儘管這是我正在尋找的,但這不會在MS Access中工作(對不起,我應該提到更早) – Imran 2012-04-20 04:21:31

+0

你有沒有可以在MS Access中使用的替代方案? CASE語句不工作在Access – Imran 2012-04-20 19:19:47

+0

@Imran我使用Linux,我沒有訪問權限。嘗試用[iif函數]替換'sum'解決方案中的比較(http://www.techonthenet.com/access/functions/advanced/iif.php)。例如:'iif(grade ='A',1,0)' – 2012-04-20 20:06:02

3

您應該使用sum代替count,並使用內部條件表達式,如:

select 
    sum(case when grade in ('A','B','C') then 1 else 0 end) as passing 
, sum(case when grade = 'A' then 1 else 0 end) as NumA 
, sum(case when grade = 'B' then 1 else 0 end) as NumB 
, sum(case when grade = 'C' then 1 else 0 end) as NumC 
from grades 

這個例子是SQL Server (see sqlfiddle here),但其他RDBMS具有類似的功能。

0

另一種可能的答案(很長,但是自我解釋):

select u.UserName 
,totalA = count(a.Grade) 
,totalB = count(b.Grade) 
,totalC = count(c.Grade) 
,totalPass = count(p.Grade) 
from 
GradeUser u 
LEFT JOIN GradeUser a on a.UserName = u.UserName 
AND a.Grade = u.Grade AND a.Class = u.Class 
AND a.Grade ='A' 
LEFT JOIN GradeUser b on b.UserName = u.UserName 
AND b.Grade = u.Grade AND b.Class = u.Class 
AND b.Grade ='B' 
LEFT JOIN GradeUser c on c.UserName = u.UserName 
AND c.Grade = u.Grade AND c.Class = u.Class 
AND c.Grade ='C' 
LEFT JOIN GradeUser p on p.UserName = u.UserName 
AND p.Grade = u.Grade AND p.Class = u.Class 
AND p.Grade IN ('A', 'B', 'C') 
GROUP BY u.UserName 
0

這裏是訪問SQL的解決方案,在兩個單獨的查詢:

每名學生的總合格成績的數目:

SELECT grades.StudentName, Count(grades.Grade) AS NumberOfPassGrades 
FROM grades 
WHERE (((grades.Grade) In ('A','B','C'))) 
GROUP BY grades.StudentName 
ORDER BY grades.StudentName; 

的總A的號碼,共有B的,總的C的:

SELECT grades.StudentName, grades.Grade, Count(grades.Grade) AS NumberOfGrades 
FROM grades 
GROUP BY grades.StudentName, grades.Grade 
ORDER BY grades.StudentName, grades.Grade; 

如果您確實只需要一個查詢,最簡單的方法是將這兩個查詢保存爲Access查詢並將它們加入StudentName。然而,一旦學生有不止一個不同的成績,你就會多次獲得NumberOfPassGrades。

+0

總通過成績查詢的作品數量是我的 然而,第二個查詢顯示每個年級,我試圖實現的是它將顯示每個用戶和列中的總和成績。 莫斯迪Mostacho得到它的權利,但唯一的問題是我相信CASE聲明不工作在MS Access – Imran 2012-04-20 19:19:19

+0

@Imran:你是說你不想在第二個查詢中的每個級別,但只有所有級別的總和,而不是?這聽起來就像是對我的第一個詢問......或者我不明白你想要做什麼。 – 2012-04-20 19:25:18