按部分值分组
我对 SQL 非常陌生,我什至不知道如何开始。 我有两个表:仓库和物品。它们的外观如下(简化):
Warehouse
ItemID | QuantityInStock | QuantityOnOrder | QuantityOnOrder2 | QuantityOnOrder3 | QuantityOnOrder4
-------+-----------------+-----------------+------------------+------------------+-----------------
1111 | 8 | 1 | 0 | 1 | 0
2222 | 3 | 0 | 0 | 0 | 0
3333 | 4 | 0 | 1 | 0 | 0
Items
ItemID | Code
-------+-----------------
1111 | abc123456-111-01
2222 | abc123456-111-02
3333 | abc123457-112-01
我需要通过 SQL 查询返回的是:
ShortCode | Quantity
----------+---------
abc123456 | 9
abc123457 | 3
- ItemID 是连接两个表的关键
- Items 表中的代码包括主要产品代码 (abc123456) 和变型 (-111-01)。我需要仅按主要产品代码对行进行分组
- ,我需要的数量来自仓库表,它等于“QuantityInStock - QuantityOnOrder - QuantityOnOrder2 - QuantityOnOrder3 - QuantityOnOrder4”。使用此我们得到 abc123456 (在 Items 表中有两个变体,ItemId 为 1111 和 2222),对于 1111,数量等于 8 minus 1 minus 0 minus 1 minus 0;对于 2222,数量等于 3 minus 0 minus 0 minus 0 minus 0 9
这可能是有史以来最糟糕的解释,所以我希望有人能理解它。
请帮忙。
I'm very new to SQL and I have no clue how to even begin with this one.
I've got two tables: Warehouse and Items. Here's how they look like(simplified):
Warehouse
ItemID | QuantityInStock | QuantityOnOrder | QuantityOnOrder2 | QuantityOnOrder3 | QuantityOnOrder4
-------+-----------------+-----------------+------------------+------------------+-----------------
1111 | 8 | 1 | 0 | 1 | 0
2222 | 3 | 0 | 0 | 0 | 0
3333 | 4 | 0 | 1 | 0 | 0
Items
ItemID | Code
-------+-----------------
1111 | abc123456-111-01
2222 | abc123456-111-02
3333 | abc123457-112-01
What I need to return via SQL query is this:
ShortCode | Quantity
----------+---------
abc123456 | 9
abc123457 | 3
- ItemID is the key to join both tables
- Code in the Items table include main product code (abc123456) and variants (-111-01). I need to group the lines by main product code only
- Quantity I need comes from Warehouse table and it equals "QuantityInStock - QuantityOnOrder - QuantityOnOrder2 - QuantityOnOrder3 - QuantityOnOrder4". Using this we get abc123456 (comes in two variants in Items table with ItemId 1111 and 2222) and Quantity is equal 8 minus 1 minus 0 minus 1 minus 0 for 1111, and 3 minus 0 minus 0 minus 0 minus 0 for 2222 which together gives 9
This is probably the worst explanation ever, so I hope there is someone that can understand it.
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您始终可以指望匹配 Code 列的前 9 个字符,那么以下查询应该可以工作。
Assuming that you can always count on matching the first 9 characters of the Code column then the following query should work.
使用标准 SQL:
未经测试,但应该可以工作。唯一潜在的问题是您在表名和列名中使用大写字母,因此您可能必须将所有表名和列名括在反引号 (`) 或方括号中,具体取决于您的数据库服务器。
编辑:如果您想过滤那些剩余数量少于一定数量的商品,只需添加:
其中
xxx
是您想要的最小数量Using standard SQL:
Not tested, but should work. Only potential issue is that you use uppercase letters in your table and column names, so you might have to enclose all table and column names in backticks (`) or square brackets depending on your DB server.
EDIT: If you want to filter those with less than a certain number of pieces left, just add:
Where
xxx
is the minimum quantity you want