需求
假定有这样得表
需要根据department,categoryid,standard 分组并且,status = 0,和status = 1 得状况来统计在库和非在库,status = 0代表在库,status = 1 代表非在库,如果将数据统计出来
departmentid categoryid standard num 在库 出库
123 517 规格1 5 4 1
123 518 规格1 8 7 1
show me the code
select departmentId,categoryId,standard,count(0) AS num,
sum((case when (status = 0) then 1 else 0 end)) AS inlib,
sum((case when (status) then 1 else 0 end)) AS outlib from device
group by departmentId,categoryId,standard