下面是插入数据的表格.
CREATE TABLE police(event_id VARCHAR(100), search_type VARCHAR(1000),
reported_by VARCHAR(100));
INSERT INTO police VALUES ('gng67g6yf', 'person','ceridigion' );
INSERT INTO police VALUES ('bfewbqfjhfb', 'person', 'ceridigion' );
INSERT INTO police VALUES ('ytftdfctg', 'n/a','ceridigion' );
INSERT INTO police VALUES ('yufvugyu', 'person','pembrokeshire', );
我想要找到的是一个查询,它将列出reported_by并在这些区域中进行搜索.如下所示:
reported_by Stopped and searched
ceridigion 2
pembrokeshire 1
最佳答案
SELECT reported_by,
SUM(CASE WHEN search_type = 'person' THEN 1 ELSE 0 END) AS `Stopped and searched`
FROM police
GROUP BY reported_by
在这里演示:
SQLFiddle