本文介绍了从购买和出售的产品中获取库存数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我一直致力于客户项目。我需要从购买的产品数量和卖出的数量中获得库存。 这里我的SQL SELECT DISTINCTROW tbl_itemmaster.product_code,tbl_itemmaster.unittype,tbl_itemmaster.color,SUM(tbl_purchasedetails.qty),SUM(tbl_saledetails.qty),(SUM(tbl_purchasedetails.qty)-SUM( tbl_saledetails.qty)) FROM(tbl_itemmaster LEFT JOIN tbl_saledetails ON tbl_itemmaster。[product_code] = tbl_saledetails。[product_code]) LEFT JOIN tbl_purchasedetails ON tbl_itemmaster。[product_code] = tbl_purchasedetails。[product_code] GROUP BY tbl_itemmaster.product_code,tbl_itemmaster.unittype,tbl_itemmaster.color 以下是sql的结果 ----------------- ------------------------------------------------ 产品代码单位类型颜色购买数量已售出数量库存 ---------------------------------- --------- ---------------------- 1001 KiloGram Red 500 500 公斤白 1033 300 733 570 公斤黑 600 公斤粉红色 9005 Kilogram Magenta 800 。 5 900 公斤绿 ------- -------------------------------------------------- -------- 产品代码 500 有三次购买条目即500 + 400 +133 = 1033购买数量 产品代码 500 只有一次销售条目,即100 = 100已售出数量 产品代码 500 假设 933 库存数量 但为什么我在售出的数量 300 ? 我的查询缺少哪里? 请帮助!!! 解决方案 我认为这是因为你正在使用DISTINCTROW任何2行具有相同的值只会返回1 I have been working on Client project. I need to get stock from Purchased product QTY and Sold QTY.Here my SQLSELECT DISTINCTROW tbl_itemmaster.product_code,tbl_itemmaster.unittype,tbl_itemmaster.color,SUM(tbl_purchasedetails.qty),SUM(tbl_saledetails.qty),(SUM(tbl_purchasedetails.qty)-SUM(tbl_saledetails.qty)) FROM (tbl_itemmaster LEFT JOIN tbl_saledetails ON tbl_itemmaster.[product_code] = tbl_saledetails.[product_code]) LEFT JOIN tbl_purchasedetails ON tbl_itemmaster.[product_code]=tbl_purchasedetails.[product_code] GROUP BY tbl_itemmaster.product_code,tbl_itemmaster.unittype,tbl_itemmaster.colorBelow is the result of sql-----------------------------------------------------------------Product Code Unit Type Color Purchase QTY Sold QTY Stock-----------------------------------------------------------------1001 KiloGram Red 500 500 Kilogram White 1033 300 733570 Kilogram Black 600 Kilogram Pink 9005 Kilogram Magenta 800.5 900 Kilogram Green -----------------------------------------------------------------Product code 500 has three times purchase entry i.e. 500+400+133 = 1033 Purchased Qty Product code 500 has only ONE time sale entry i.e. 100 = 100 Sold Qty Product code 500 suppose to has 933 Stock Qty BUT why I getting 300 in sold qty ? Where my query is lacking? PLEASE HELP !!! 解决方案 I think its because you are using the DISTINCTROW any 2 rows with the same values will only return 1 这篇关于从购买和出售的产品中获取库存数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-13 21:35