SELECT
mc.med_idlab
, mi.namelab
, mu.name
,mi.unitprice
,mc_ii.remain AS num1,(mc_ii.remain*mi.unitprice) AS price1
,mro.med_amount AS num2,(mro.med_amount*mi.unitprice) AS price2
,(mc_ii.remain+mro.med_amount) AS num3
,((mc_ii.remain*mi.unitprice)+(mro.med_amount*mi.unitprice)) AS price3
,SUM(mi1.confirm) AS num4,(SUM(mi1.confirm)*mi.unitprice) AS price4
,mr.remain AS num5 ,(mr.remain*mi.unitprice ) AS price5
FROM
med_cal mc
INNER JOIN med_items mi ON mi.med_items_id=mc.med_idlab
INNER JOIN med_unit mu ON mu.med_unit_id=mi.idunit
INNER JOIN med_receive_order mro ON mro.med_idlab=mc.med_idlab
INNER JOIN med_inorder mi1 ON mi1.idlab=mc.med_idlab
INNER JOIN med_remain mr ON mr.med_idlab=mc.med_idlab
LEFT JOIN (
SELECT *
FROM med_cal
WHERE date_update BETWEEN DATE_ADD('2017-02-01', INTERVAL -1 MONTH)
AND DATE_ADD('2017-02-16', INTERVAL -1 MONTH)
) mc_ii ON mc.med_idlab = mc_ii.med_idlab
where mc.date_update between '2017-02-01'AND '2017-02-16'
GROUP BY mc.med_idlab
ORDER BY mi.med_items_id ASC
ผมว่า sql มันแปลกๆ ลอง group by ให้ครบหน่อยครับ
หรือลอง
Code (SQL)
Select bla bla bla, aa.sumconfirm, aa.sumconfirm*unitprice as price4
From maindata mm
Left outer join (
Select id, Sum(confirm) sumconfirm from aaa group by id
) aa On mm.id = aa.id