2010-09-28 66 views
2

我有兩個表,其中一個包含作者deatils和其他文章詳細信息。一位作者可能有許多文章。在文章表中是一個標題爲「決定」的字段在SQL中的一個字段上的多個條件計數

決策字段可以是「接受」或「拒絕」。我想要一個查詢輸出一個作者姓名和ID的表格,然後輸入他們提交的數量,接受和拒絕的數量。問題出現了(我對SQL比較陌生,所以請耐心等待),因爲此刻我正在使用WHERE Article.Decision =「Accept」或類似的東西,並且無法找到如何在其他生成的字段中構建獨立依賴項沒有破壞語法。任何幫助非常感謝!

道歉,這裏是當前的SQL語句,它返回的作者有一個匹配和計數。只是一個表達與有一個空計數就可以了作者(然後我可以追加單獨查詢來構建整個事情)

SELECT Authors.[Corresponding Author URN], Authors.[Corresponding Author Surname], Count(Articles.Decision) AS CountOfDecision 
FROM Authors LEFT OUTER JOIN Articles ON Authors.[Corresponding Author URN] = Articles.[Corresponding Author URN] 
WHERE (((Articles.Decision)="Rejected")) 
GROUP BY Authors.[Corresponding Author URN], Authors.[Corresponding Author Surname]; 

甕是ID返回此表。

+0

對於此字段只有兩個可能的值,您可以考慮一個整數字段並將值限制爲Null,True和False(Null,-1,0)。 – 2010-09-29 00:56:13

回答

4

你可以使用一個case語句:

select sum(case when Decision = 'Rejected' then 1 else 0 end) as RejectedCount 
,  sum(case when Decision = 'Denied' then 1 else 0 end) as DeniedCount 
,  sum(case when Decision = 'Discarded' then 1 else 0 end) as DiscardedCount 
,  sum(case when Decision = 'Buried' then 1 else 0 end) as BuriedCount 

編輯:在Access中,您可以使用iif代替case,如:

select sum(iif(Decision="Delayed",1,0)) as DelayedCount 
,  sum(iif(Decision="Ignored",1,0)) as IgnoredCount 
,  sum(iif(Decision="Repulsed",1,0)) as RepulsedCount 
,  sum(iif(Decision="Declined",1,0)) as DeclinedCount 
+0

謝謝,但根據上述...似乎訪問(我被迫使用)不支持案件陳述。 * Rage * – Dave 2010-09-28 10:49:29

+0

@Dave:編輯訪問 – Andomar 2010-09-28 10:55:00

0
SELECT 
     Authors.[Corresponding Author URN], 
     Authors.[Corresponding Author SURNAME], 
     SUM(CASE WHEN decision = 'Rejected' THEN 1 ELSE 0 END) Rejected, 
     SUM(CASE WHEN decision = 'Accepted' THEN 1 ELSE 0 END) Accepted 
FROM 
     Authors LEFT OUTER JOIN Articles ON Authors.[Corresponding Author URN] = Articles.[Corresponding Author URN] 
GROUP BY 
     Authors.[Corresponding Author URN], Authors.[Corresponding Author Surname]; 
+0

非常感謝您的回覆......我仍然得到一個語法錯誤雖然(缺少運算符)...任何想法? – Dave 2010-09-28 10:45:51

+0

看來Access沒有CASE語句!嘆! – Dave 2010-09-28 10:48:53

+0

可能可以用switch語句替換case語句嗎?如: – Dave 2010-09-28 10:52:47

0

你也可以使用一個交叉表,但我不會爲此發佈SQL,因爲我總是使用QBE創建交叉表。該向導應該引導你通過它。結果將是名稱列第一行,然後是Decision列中的每個值的列(Accepted列和Rejected列)以及每個列的計數。

相關問題