如何从 SQL Server 2000 的表中提取当前库存

发布于 2024-12-22 09:55:50 字数 761 浏览 0 评论 0原文

我正在使用 Visual Studio 2005 和 SQL Server 2000 在 vb.net 中进行制作和应用。我必须跟踪库存。添加后,库存的状态设置为“未售出”。当某些库存被移除时,交易会将库存状态保持为“已售出”。对于已过期的股票,状态也可以是“已过期”,当向公司认领过期股票时,状态为“已认领”...现在,我想从这一切中提取当前手中的股票。在基本级别上,有两个状态值,即“已售出”和“未售出”,我使用以下查询从数据库中提取结果,但它不会计算已售出和未售出商品的差异...

 select category, item_name, 
        sum(crtns_added) as Cartons, sum(pieces_added) as Pieces, 
        sum(total_price) 
 from Stock 
 where status = 'unsold' 
 group by category, item_name

我也尝试过这个

select category, item_name, (sum(crtns_added) - sum(crtns_removed)) as Cartons, 
       (sum(pieces_added)- sum(pieces_removed)) as Pieces, 
       sum(total_price) 
from Stock 
where status = 'unsold' or status = 'sold' 
group by category, item_name

I'm making and application in vb.net using Visual Studio 2005 and SQL Server 2000. I have to keep track of the stock. The stock when added has its status set as "Unsold". When some stock is removed then the transaction keeps the status of stock as "Sold". The status can also be "Expired" for the stock that has been expired and when expired stock is claimed to the company then the status is "claimed"...Now from all this I want to extract the stock which is currently in hand. At a basic level with two status values i.e. "Sold" and "Unsold" I'm using the following query to extract the results from database but it doesn't calculate the difference sold and unsold items...

 select category, item_name, 
        sum(crtns_added) as Cartons, sum(pieces_added) as Pieces, 
        sum(total_price) 
 from Stock 
 where status = 'unsold' 
 group by category, item_name

I have also tried this

select category, item_name, (sum(crtns_added) - sum(crtns_removed)) as Cartons, 
       (sum(pieces_added)- sum(pieces_removed)) as Pieces, 
       sum(total_price) 
from Stock 
where status = 'unsold' or status = 'sold' 
group by category, item_name

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

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

发布评论

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

评论(1

又爬满兰若 2024-12-29 09:55:50

您需要使用两个子选择来选择具有适当值的项目 - 类似于:

SELECT
    category, 
    item_name, 
    (SELECT SUM(crtns_added) FROM dbo.Stock s1 WHERE s1.ItemID = s.ItemID AND s1.Status = 'unsold') 'Unsold Cartons',
    (SELECT SUM(crtns_added) FROM dbo.Stock s2 WHERE s2.ItemID = s.ItemID AND s2.Status = 'sold') 'Sold Cartons'
 FROM 
    dbo.Stock s
 GROUP BY 
    category, item_name

假设有一个 itemId 列唯一标识您的项目 - 某种主键。通过这种技术,您可以获得已售出或未售出的纸箱或件的总和 - 无论您需要什么。

You need to use two sub-selects to select the items with the appropriate values - something like:

SELECT
    category, 
    item_name, 
    (SELECT SUM(crtns_added) FROM dbo.Stock s1 WHERE s1.ItemID = s.ItemID AND s1.Status = 'unsold') 'Unsold Cartons',
    (SELECT SUM(crtns_added) FROM dbo.Stock s2 WHERE s2.ItemID = s.ItemID AND s2.Status = 'sold') 'Sold Cartons'
 FROM 
    dbo.Stock s
 GROUP BY 
    category, item_name

This assume there's an itemId column that uniquely identifies your items - some kind of a primary key. With this technique, you can get the sum of the sold or unsold cartons or pieces - whatever you need.

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