按相似代码/类别对产品进行求和和分组
我的问题是,在供应商的发票中,我有相同的产品但名称不同。我想要达到的目标是将它们分组到单个产品代码/类别中。你觉得可能吗?
我想对 SQL Server 表中的特定记录进行求和。我有这样的东西
描述 | 数量 |
---|---|
电话 X | 10 |
AB 笔记本电脑 B | 20 |
X 电话 | 15 |
笔记本电脑 C | 20 |
AB 电话 X | 5 |
我必须将“电话”和“笔记本电脑”相加,以获得这样的表格
描述 | 数量 |
---|---|
电话 | 30 |
笔记本电脑 | 40 |
I不知道使用哪个查询是正确的。问题是“笔记本电脑”和“电话”这两个词包含其他单词,我无法对它们求和。
有人可以帮我吗?
My problem is that in the supplier's invoices I have the same product with different names. The goal I'd like to reach is to group them in a single product code/category. Do you think is possible?
I want to sum specific record in my SQL Server table. I have something like this
Description | Quantity |
---|---|
Phone X | 10 |
AB Laptop B | 20 |
X Phone | 15 |
Laptop C | 20 |
AB Phone X | 5 |
And I have to sum "Phone" and "Laptop", to obtain a table like this
Description | Quantity |
---|---|
Phone | 30 |
Laptop | 40 |
I have no idea which query is correct to use. The problem is the words "Laptop" and "Phone" contains other words, and I can't sum them.
Can someone help me, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
警告:
正如其他人提到的,解析字符串值可能会导致数据问题。也许不是今天,而是未来。例如,如果有人列出“带有内置无线手机充电器的笔记本电脑”怎么办?根据此查询的使用方式,可能会导致严重问题。
推荐的数据分类方法:
我使用过许多外部数据源,如果是我,我建议执行以下操作之一:
这两种方法各有利弊,但都会始终提供准确的数据。
根据描述中的关键字按类别进行分组 出于
公平的警告,如果您不能让供应商更改其数据源,我会这样做:
Word of Warning:
As others mentioned, parsing string values this will probably lead to data issues. Maybe not today, but down the road. For example, what if someone listed "Laptop w/ built in wireless phone charger". Depending on how this query is used, could cause significant issues.
Recommended Approach to Categorizing Data:
I've worked with many external data feeds and if it were me, I'd recommend doing one of the following:
There are pros and cons to both approaches, but both would consistently provide accurate data.
Group By Category from Key Word in Description
With that fair word of caution, here's how I'd do it if you can't have your vendors change their data feeds:
斯蒂芬的回答是我的第一个想法+1,但是,我倾向于将这些项目保留在通用映射表中而不是代码中......太多接触点
想象
@Map
是一个物理表示例< /strong>
结果
Stephan's answer was my first thought +1, however, I tend prefer to keep such items in generic mapping table and not code ... too many touchpoints
Imagine
@Map
was a physical tableExample
Results
当然,这可以单独用 SQL 完成,但由于您已经使用 vb.net 进行标记,也许您需要 .NET 解决方案。另外,由于您没有提供代码,我将弥补一些
如果您添加更多产品,这将会中断。它将描述中带有“电话”的任何东西识别为电话,将其他任何东西识别为笔记本电脑。
Of course this could be done in SQL alone, but since you have tagged with vb.net, maybe you want a .NET solution. Also, since you supplied no code, I will make some up
If you add more products, this will break. It recognizes anything with "phone" in the description in it as a Phone, and anything else as a laptop.