select agent_id '总仓ID',s.title '总仓名称', material_id '物料ID',m.title '物料名称',m.specification '物料规格', sum(case when agent_order_id>0 then 1 else 0 end) as '进货数量', sum(case when agent_order_id>0 and order_id>0 then 1 else 0 end) as '销售数量', r.pid, rg.aname from shop_material_detail md join agent s on md.agent_id=s.id join material m on md.material_id=m.id join region r on r.id=s.region_id join region rg on r.pid=rg.id /*where md.created BETWEEN UNIX_TIMESTAMP('2022-10-30') and UNIX_TIMESTAMP('2022-12-31')*/ where md.created between UNIX_TIMESTAMP('${tstart1}') and UNIX_TIMESTAMP('${tend1}')+86400 and agent_order_id>0 and (rg.city in (select city from fr_access_app_v2 where username = '${pa}')) GROUP BY md.agent_id,material_id,r.pid, rg.aname |