从项目表和每日移动表创建月度报告

时间:2016-01-29 17:12:48

标签: mysql

请,我的请求是从项目表和每日交易表创建月度报表表

here

1 个答案:

答案 0 :(得分:0)

试试这个:

select i.id,i.code.i.item_name ,
       sum(if(m.action='purchased',m.quantity,0)) as total_purchased,
       sum(if(m.action='sold',m.quantity,0))*-1 as total_sold,
       sum(if(m.action='purchased',m.quantity,0))+sum(if(m.action='sold',m.quantity,0)) as balance
from items i
inner join movements m on i.code=m.code
group by i.code;