如何创建sql Balance计算

时间:2017-08-07 08:12:34

标签: mysql

我想创建sql余额计算我有4个表这样的股票,stocklist,sub,子列表结果不工作。平衡错误 看我的代码

SELECT cc.mm,cc.dd,cc.name,cc.q1,cc.q2,cc.staff,(select sum(q1) - sum(q2) 
    from (SELECT s1.qty AS q1,"" AS q2,i.item_id,s2.receive_date as dd,concat('A',s2.id) AS mm
             FROM  mitem i 
             JOIN mstocklist s1 ON s1.item_id=i.item_id
             JOIN mstock s2 ON s2.id=s1.stock_id
             JOIN msupplier sp ON sp.supplier_id=s2.supplier_id
             WHERE s2.receive_date BETWEEN '2016-10-01' AND '2017-09-30'
             AND i.item_id='3' UNION SELECT "" AS q1,s1.qty AS q2,i.item_id,s2.receive_date as dd,concat('B',s2.id) AS mm
             FROM mitem i 
             JOIN msublist s1 ON s1.item_id=i.item_id
             JOIN msub s2 ON s2.id=s1.sub_id
             WHERE s2.receive_date BETWEEN '2016-10-01' AND '2017-09-30'
             AND i.item_id='3') as a where a.item_id=cc.item_id and a.mm <= cc.mm) as balance FROM(SELECT i.item_id,s2.receive_date as dd,i.name,s1.qty AS q1,"" AS q2,s2.staff_receive AS staff,concat('A',s2.id) AS mm FROM mitem i 
             JOIN mstocklist s1 ON s1.item_id=i.item_id
             JOIN mstock s2 ON s2.id=s1.stock_id
             JOIN msupplier sp ON sp.supplier_id=s2.supplier_id
             WHERE s2.receive_date BETWEEN '2016-10-01' AND '2017-09-30'
             AND i.item_id='3' UNION SELECT i.item_id,s2.receive_date as dd,i.name,"" AS q1,s1.qty AS q2,s2.staff_draw AS staff,concat('B',s2.id) AS mm FROM mitem i 
             JOIN msublist s1 ON s1.item_id=i.item_id
             JOIN msub s2 ON s2.id=s1.sub_id
             WHERE s2.receive_date BETWEEN '2016-10-01' AND '2017-09-30'
             AND i.item_id='3') as cc ORDER BY dd ASC

谁能帮助我。

enter image description here

1 个答案:

答案 0 :(得分:0)

如果我没有记错(你的代码很难得到):有FROM没有选择。 这一个:

FROM (SELECT i.item_id
             , s2.receive_date AS dd
             , i.name
             , s1.qty AS q1
             , "" AS q2
             , (s1.qty*i.unitcost) AS cost
             , s2.staff_receive AS staff
             , s2.id AS mm

在你的第13行