动态库存缺少价值且没有销售

发布于 2025-01-06 23:19:07 字数 2205 浏览 0 评论 0原文

我有以下查询,

QryStockOnHand
SELECT QrySaleTot.Item, QrySaleTot.ProductID, [QryStockLevel].[Stock]-[QrySaleTot].[Quantity] AS StockOnHand
FROM QryStockLevel INNER JOIN QrySaleTot ON QryStockLevel.ProductID = QrySaleTot.ProductID;

QrySaleTot
SELECT TblProduct.Item, Sum(TblTotalSale.Size) AS Quantity, TblProduct.ProductID
FROM TblProduct INNER JOIN TblTotalSale ON TblProduct.[ProductID] = TblTotalSale.[ProductID]
GROUP BY TblProduct.Item, TblProduct.ProductID;

QryStockLevel
SELECT TblStock.ProductID, Sum(TblStock.StockLevel) AS Stock, TblProduct.Item
FROM TblStock INNER JOIN TblProduct ON TblStock.ProductID = TblProduct.ProductID
GROUP BY TblStock.ProductID, TblProduct.Item;

当我运行 QryStockonHand 并且没有销售任何产品时,该产品不会出现在查询结果中...

示例数据

TblStock
StockID ProductID   StockLevel
138 1   528
139 3   528
140 5   528
141 9   528
142 7   528
143 18  80
144 30  72
145 34  72
146 33  72
147 32  200 
148 22  80
149 19  80
150 23  80
151 20  80



TblProduct
ProductID   Item    Price   StockDelivery   PriceSmall  Large   Small
1   Carling         £2.50   528         £1.40           2   1
3   Carlsburg   £2.70   528         £1.60           2   1
5   IPA         £2.30   528         £1.20           2   1
7   StrongBow   £2.80   528         £1.65           2   1
9   RevJames    £2.45   528         £1.30           2   1
11  Becks           £2.90   72                      1   
12  WKDBlue     £2.80   72              1   
13  WKDRed          £2.80   72              1   
14  SmirnoffIce £2.80   72              1   



TblTotalSale
TotalSalesID    ProductID   SalePrice   Day Time    Size
576 1   £1.40   19/02/2012  15:34:24    1
528 1   £2.50   09/02/2012  14:44:44    2
530 1   £1.40   09/02/2012  14:44:44    1
565 1   £2.50   19/02/2012  15:34:24    2
567 1   £1.40   19/02/2012  15:34:24    1
570 3   £2.70   19/02/2012  15:34:24    2
571 3   £1.60   19/02/2012  15:34:24    1
577 3   £2.70   19/02/2012  15:34:24    2
578 3   £1.60   19/02/2012  15:34:24    1
533 3   £2.70   09/02/2012  14:44:44    2
534 3   £1.60   09/02/2012  14:44:44    1

任何想法为什么...我猜这是一个空的东西,它将无销售视为不存在的事情,而不是零销售......知道我如何解决它吗?

谢谢萨姆

I have the following queries,

QryStockOnHand
SELECT QrySaleTot.Item, QrySaleTot.ProductID, [QryStockLevel].[Stock]-[QrySaleTot].[Quantity] AS StockOnHand
FROM QryStockLevel INNER JOIN QrySaleTot ON QryStockLevel.ProductID = QrySaleTot.ProductID;

QrySaleTot
SELECT TblProduct.Item, Sum(TblTotalSale.Size) AS Quantity, TblProduct.ProductID
FROM TblProduct INNER JOIN TblTotalSale ON TblProduct.[ProductID] = TblTotalSale.[ProductID]
GROUP BY TblProduct.Item, TblProduct.ProductID;

QryStockLevel
SELECT TblStock.ProductID, Sum(TblStock.StockLevel) AS Stock, TblProduct.Item
FROM TblStock INNER JOIN TblProduct ON TblStock.ProductID = TblProduct.ProductID
GROUP BY TblStock.ProductID, TblProduct.Item;

When I run the QryStockonHand and no sales of a product have been made then the porduct does not appear in the result of the query...

Sample Data

TblStock
StockID ProductID   StockLevel
138 1   528
139 3   528
140 5   528
141 9   528
142 7   528
143 18  80
144 30  72
145 34  72
146 33  72
147 32  200 
148 22  80
149 19  80
150 23  80
151 20  80



TblProduct
ProductID   Item    Price   StockDelivery   PriceSmall  Large   Small
1   Carling         £2.50   528         £1.40           2   1
3   Carlsburg   £2.70   528         £1.60           2   1
5   IPA         £2.30   528         £1.20           2   1
7   StrongBow   £2.80   528         £1.65           2   1
9   RevJames    £2.45   528         £1.30           2   1
11  Becks           £2.90   72                      1   
12  WKDBlue     £2.80   72              1   
13  WKDRed          £2.80   72              1   
14  SmirnoffIce £2.80   72              1   



TblTotalSale
TotalSalesID    ProductID   SalePrice   Day Time    Size
576 1   £1.40   19/02/2012  15:34:24    1
528 1   £2.50   09/02/2012  14:44:44    2
530 1   £1.40   09/02/2012  14:44:44    1
565 1   £2.50   19/02/2012  15:34:24    2
567 1   £1.40   19/02/2012  15:34:24    1
570 3   £2.70   19/02/2012  15:34:24    2
571 3   £1.60   19/02/2012  15:34:24    1
577 3   £2.70   19/02/2012  15:34:24    2
578 3   £1.60   19/02/2012  15:34:24    1
533 3   £2.70   09/02/2012  14:44:44    2
534 3   £1.60   09/02/2012  14:44:44    1

Any Idea why... I guess it is a null thing, where it is seeing the no sales as a non existent thing, instead of a zero sales.... any idea how i could fix it?

Thanks

Sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡忘如思 2025-01-13 23:19:07

使用左外连接而不是内连接,这将告诉它从连接上的左表中获取所有行,而不是内连接,后者仅返回在两个表中都有值的行。

我不知道 QryStockLevel 字段,但您的查询应该如下所示:

SELECT QryStockLevel.Item, QryStockLevel.ProductID, [QryStockLevel].[Stock]-NZ([QrySaleTot].[Quantity],0) AS StockOnHand
FROM QryStockLevel LEFT OUTER JOIN QrySaleTot ON QryStockLevel.ProductID = QrySaleTot.ProductID;

请注意,当 qrysaletot 没有行时,NZ 函数会处理数量上的空值。

Instead of an inner join, use a left outer join, which will tell it to grab all of the rows from the left hand table on the join, instead of an inner join, which returns only rows which have values in both tables.

I don't know the QryStockLevel fields, but your query should look something like this:

SELECT QryStockLevel.Item, QryStockLevel.ProductID, [QryStockLevel].[Stock]-NZ([QrySaleTot].[Quantity],0) AS StockOnHand
FROM QryStockLevel LEFT OUTER JOIN QrySaleTot ON QryStockLevel.ProductID = QrySaleTot.ProductID;

Note the NZ function to handle a null on the Quantity when qrysaletot does not have a row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文