我有一个这样的数据库表:
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 :(得分: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
答案 1 :(得分:0)
select room_name, animal, count(*)
from table
group by room_name, animal