表结构和数据可用here
我想要的输出是
但我的查询输出是
请问我怎么能得到我想要的输出?
SQL查询在
之下SELECT
(select product_name from ct_product where id=stock_in.ct_prod_id) as name,
stockin.ct_prod_status,
stockin.ct_prod_catgry,
IFNull(stockin.stock_in, 0) stock_in,
IFNull(stockout.stock_out, 0) stock_out,
IFNull(stockin.stock_in, 0)-IFNull(stockout.stock_out, 0) stockinhand
FROM stock_in
LEFT JOIN
(
SELECT
SUM(quantity) stock_in,
ct_prod_id,
ct_prod_catgry,
ct_prod_status
FROM stock_in
group by ct_prod_catgry, ct_prod_id, ct_prod_status
) stockin ON stockin.ct_prod_id = stock_in.ct_prod_id
LEFT JOIN
(
SELECT
SUM(quantity) stock_out,
ct_prod_id,
ct_prod_catgry,
ct_prod_status
FROM stock_out
group by ct_prod_catgry, ct_prod_id, ct_prod_status
) stockout ON stockout.ct_prod_id = stock_in.ct_prod_id
where stockout.ct_prod_catgry=stock_in.ct_prod_catgry and
stockout.ct_prod_status=stock_in.ct_prod_status
答案 0 :(得分:3)
您需要在整个查询中添加一个组,并将SUM添加到总计的项目中:
SELECT
(select product_name from ct_product where id=stock_in.ct_prod_id) as name,
stockin.ct_prod_status,
stockin.ct_prod_catgry,
SUM(IFNull(stockin.stock_in, 0)) stock_in,
SUM(IFNull(stockout.stock_out, 0)) stock_out,
SUM(IFNull(stockin.stock_in, 0)-IFNull(stockout.stock_out, 0)) stockinhand
FROM stock_in
LEFT JOIN
(
SELECT
SUM(quantity) stock_in,
ct_prod_id,
ct_prod_catgry,
ct_prod_status
FROM stock_in
group by ct_prod_catgry, ct_prod_id, ct_prod_status
) stockin ON stockin.ct_prod_id = stock_in.ct_prod_id
LEFT JOIN
(
SELECT
SUM(quantity) stock_out,
ct_prod_id,
ct_prod_catgry,
ct_prod_status
FROM stock_out
group by ct_prod_catgry, ct_prod_id, ct_prod_status
) stockout ON stockout.ct_prod_id = stock_in.ct_prod_id
where stockout.ct_prod_catgry=stock_in.ct_prod_catgry and
stockout.ct_prod_status=stock_in.ct_prod_status
group by name, stockin.ct_prod_status, stockin.ct_prod_catgry
答案 1 :(得分:0)
两个问题两点考虑(POC)
http://sqlfiddle.com/#!9/61d62e/47/0在select查询中使用获取名称 http://sqlfiddle.com/#!9/61d62e/49/0使用联接来获取名称[我是这种方法的忠实粉丝]
SELECT
CP.Product_Name as name, #I like joins instead of selects at this level
stockin.ct_prod_status,
stockin.ct_prod_catgry,
IFNull(stockin.stock_in, 0) stock_in,
IFNull(stockout.stock_out, 0) stock_out,
IFNull(stockin.stock_in, 0)-IFNull(stockout.stock_out, 0) stockinhand
FROM (select distinct ct_prod_ID, ct_prod_status, ct_prod_Catgry from stock_in) stock_in #needed to get a distinct list of ct_** to join
INNER JOIN ct_product cp
on stock_in.ct_prod_ID = cp.ID
LEFT JOIN (SELECT SUM(quantity) stock_in
, ct_prod_id
, ct_prod_catgry
, ct_prod_status
FROM stock_in
GROUP BY ct_prod_catgry, ct_prod_id, ct_prod_status
) stockin
ON stockin.ct_prod_id = stock_in.ct_prod_id
and stockin.ct_prod_status = stock_in.ct_prod_status #Missing
and stockin.ct_prod_catgry = stock_in.ct_prod_catgry #Missing
LEFT JOIN (SELECT SUM(quantity) stock_out
, ct_prod_id
, ct_prod_catgry
, ct_prod_status
FROM stock_out
GROUP BY ct_prod_catgry, ct_prod_id, ct_prod_status
) stockout
ON stockout.ct_prod_id = stock_in.ct_prod_id
and stockout.ct_prod_catgry=stock_in.ct_prod_catgry #no where just and
and stockout.ct_prod_status=stock_in.ct_prod_status #no where just and