简体   繁体   English

Microsoft Access查询设计

[英]Microsoft access Query design

SELECT 
    Item.Itemno 
    , SUM(Delivered_item.qtyordered) AS SumOfqtyordered 
    , SUM(Item_issued.qtyissued) AS SumOfqtyissued
FROM  
    Item  
    INNER JOIN Delivered_item ON Item.Itemno = Delivered_item.itemno 
    INNER JOIN Item_issued ON Item.Itemno = Item_issued.Itemno
GROUP BY  
    Item.Itemno;

This is my sql view. 这是我的SQL视图。 In my database, delivered_item has 7 records whilst item_issued only has 2 records. 在我的数据库, delivered_item有7条记录,而item_issued只有2条记录。 When I perform SUM , the total for the qtyissued gets wrong, with 1 record( qtyissued ) multiply by 3 and another record multiply by 4, due to the delivered_item having 7 records(with 2 same itemno )...how can solve this... 当我执行SUM ,总为qtyissued得到错误的,与1个结果( qtyissued )乘以3和4另一记录乘法,由于delivered_item具有7条记录(具有2个相同itemno )...如何能够解决这个问题。 ..

You need to use sub-queries: 您需要使用子查询:

SELECT *
FROM item i
INNER JOIN
  (SELECT itemno,
          Sum(qtyordered)
   FROM delivereditem
   GROUP BY itemno) d ON d.itemno = i.itemno
INNER JOIN
  (SELECT itemno,
          Sum(qtyissued)
   FROM itemissued
   GROUP BY itemno) iss ON iss.itemno = i.itemno

If you think you might have to do this a lot, turn each of the subqueries into a view and use that instead (saves on typing) 如果您认为您可能需要执行很多操作,请将每个子查询转换为视图并使用该视图(节省键入操作)

EDIT: Added the group by's in the subqueries. 编辑:在子查询中添加了分组依据。 Sorry about that! 对于那个很抱歉!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM