bo_reception_process_record表中有process_id,attend,status,people_num字段,现在想根据process_id筛选出来的数据,获取到attend = 2时attend = 1时status = 10时status = 20不同状态下,计算people_num的总数,请帮我写一个sql
时间: 2024-03-08 14:49:02 浏览: 40
好的,以下是根据您的要求写的 SQL 语句:
```
SELECT
SUM(IF(attend = 1 AND status = 10, people_num, 0)) AS attend_1_status_10_num,
SUM(IF(attend = 1 AND status = 20, people_num, 0)) AS attend_1_status_20_num,
SUM(IF(attend = 2 AND status = 10, people_num, 0)) AS attend_2_status_10_num,
SUM(IF(attend = 2 AND status = 20, people_num, 0)) AS attend_2_status_20_num
FROM
bo_reception_process_record
WHERE
process_id = 'your_process_id' AND attend = 2;
```
以上 SQL 语句中,使用了 `IF` 函数对 `people_num` 进行了分类求和,分别计算了 `attend = 1` 且 `status = 10`,`attend = 1` 且 `status = 20`,`attend = 2` 且 `status = 10`,`attend = 2` 且 `status = 20` 四种情况下的总人数。同时,增加了 `attend = 2` 的筛选条件。您只需要将 `your_process_id` 替换成您想要筛选的 `process_id` 即可。
阅读全文