简单库存SQL查询未列出所有产品MS-ACCESS

时间:2017-07-28 01:53:25

标签: sql database ms-access inventory stock

所以我有这个简单的SELECT查询: design view of the query

SELECT tblProducts.product_ID, Sum(tblIntakes.intake_QTY)-Sum(tblExits.exit_QTY) AS Stock
FROM (tblProducts INNER JOIN tblExits ON tblProducts.product_ID = tblExits.product_ID) 
  INNER JOIN tblIntakes ON tblProducts.product_ID = tblIntakes.product_ID
GROUP BY tblProducts.product_ID;

它没有列出所有产品。它只列出那些有摄入和退出的产品。这没用,因为我想知道所有产品的库存水平。

这是我从查询中获得的:

| product_ID | Stock |
| 1          | 4     |

这是我不想要的

| product_ID | Stock |
| 1          | 4     |
| 2          | 10    |
| 3          | 0     |

这应该不是很复杂,但我是新手访问和SQL,这让我很头疼。任何帮助将不胜感激

这些是记录:

产品

| product_ID | product_Name |
| 1          | Pencil       |
| 2          | Book         |
| 3          | Marker       |

摄入

| intake_ID | intake_Date | product_ID  | intake_QTY |
| 1         | 20/07/2017  | 1           | 10         |
| 2         | 20/07/2017  | 2           | 10         |

离开

| exit_ID | exit_Date  | product_ID | exit_QTY |
| 1       | 21/07/2017 | 1          | 6        |

1 个答案:

答案 0 :(得分:1)

您正在寻找left joinnz()

SELECT tblProducts.product_ID,
       nz(Sum(tblIntakes.intake_QTY), 0) - nz(Sum(tblExits.exit_QTY)) AS Stock
FROM (tblProducts LEFT JOIN
      tblExits
      ON tblProducts.product_ID = tblExits.product_ID
     ) LEFT JOIN
      tblIntakes
      ON tblProducts.product_ID = tblIntakes.product_ID
GROUP BY tblProducts.product_ID;