2011-09-21 53 views
0

我有一個數據庫表這樣每個元素的數量:SQL查詢來計數的對象

Room Name  ||  Animal 
---------------------------- 
Room1 || Cat 
Room1 || Dog 
Room1 || Dog 
Room2 || Cat 
Room2 || Cat 
Room2 || Dog 
Room2 || Dog 

我想用一個SQL查詢和結果看這個表我想算的數在房間裏的每個動物:

Room1: 1 cat 2 dog 
Room2: 2 cats 2 dog 

(輸出格式並不重要)

是否有一個SQL查詢,可以幫助?或者我應該閱讀整個表並以編程方式過濾它?

感謝您的幫助

回答

0
SELECT [Room Name], [Animal], COUNT(*) FROM TableName GROUP BY [Room Name], [Animal] 

這將返回

Room 1 | Cat | 1 
Room 1 | Dog | 2 
Room 2 | Cat | 2 
Room 2 | Dog | 2 
0
select room_name, animal, count(*) 
from table 
group by room_name, animal