按相似代码/类别对产品进行求和和分组

发布于 2025-01-17 03:22:02 字数 704 浏览 0 评论 0原文

我的问题是,在供应商的发票中,我有相同的产品但名称不同。我想要达到的目标是将它们分组到单个产品代码/类别中。你觉得可能吗?

我想对 SQL Server 表中的特定记录进行求和。我有这样的东西

描述数量
电话 X10
AB 笔记本电脑 B20
X 电话15
笔记本电脑 C20
AB 电话 X5

我必须将“电话”和“笔记本电脑”相加,以获得这样的表格

描述数量
电话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

DescriptionQuantity
Phone X10
AB Laptop B20
X Phone15
Laptop C20
AB Phone X5

And I have to sum "Phone" and "Laptop", to obtain a table like this

DescriptionQuantity
Phone30
Laptop40

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 技术交流群。

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

发布评论

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

评论(3

七分※倦醒 2025-01-24 03:22:02

警告:

正如其他人提到的,解析字符串值可能会导致数据问题。也许不是今天,而是未来。例如,如果有人列出“带有内置无线手机充电器的笔记本电脑”怎么办?根据此查询的使用方式,可能会导致严重问题。

推荐的数据分类方法:

我使用过许多外部数据源,如果是我,我建议执行以下操作之一:

  1. 定义您自己的产品类别列表,并让您的供应商在其数据源中映射到它们
  2. 要求您的供应商将他们自己的内部产品类别添加到数据源中,然后将他们的类别映射到您自己的类别。

这两种方法各有利弊,但都会始终提供准确的数据。

根据描述中的关键字按类别进行分组 出于

公平的警告,如果您不能让供应商更改其数据源,我会这样做:

SELECT Category
    ,TotalQuantity = SUM(Quantity)
FROM YourTable AS A
CROSS APPLY (
    SELECT Category = CASE
                        WHEN [Description] LIKE '%Phone%' THEN 'Phone'
                        WHEN [Description] LIKE '%Laptop%' THEN 'Laptop'
                        ELSE 'Other'
                    END
) AS B
GROUP BY Category

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:

  1. Define a list of your own product categories and have your vendors map to them in their data feeds
  2. Ask your vendors to add their own internal product category to the data feed, and then map their categories to your own

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:

SELECT Category
    ,TotalQuantity = SUM(Quantity)
FROM YourTable AS A
CROSS APPLY (
    SELECT Category = CASE
                        WHEN [Description] LIKE '%Phone%' THEN 'Phone'
                        WHEN [Description] LIKE '%Laptop%' THEN 'Laptop'
                        ELSE 'Other'
                    END
) AS B
GROUP BY Category
不打扰别人 2025-01-24 03:22:02

斯蒂芬的回答是我的第一个想法+1,但是,我倾向于将这些项目保留在通用映射表中而不是代码中......太多接触点

想象@Map是一个物理表

示例< /strong>

 Declare @Map Table (Item varchar(50))
 Insert Into @Map values
 ('Phone')
,('Laptop')
,('Cable')
,('Battery')


Select ItemGrp = coalesce(B.Item,'Undefined')
      ,Quantity = sum([Quantity])
from   YourTable A
Left Join  @Map B on [Description] like '%'+Item+'%'
Group By B.Item 

结果

ItemGrp Quantity
Laptop  40
Phone   30

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 table

Example

 Declare @Map Table (Item varchar(50))
 Insert Into @Map values
 ('Phone')
,('Laptop')
,('Cable')
,('Battery')


Select ItemGrp = coalesce(B.Item,'Undefined')
      ,Quantity = sum([Quantity])
from   YourTable A
Left Join  @Map B on [Description] like '%'+Item+'%'
Group By B.Item 

Results

ItemGrp Quantity
Laptop  40
Phone   30
隱形的亼 2025-01-24 03:22:02

当然,这可以单独用 SQL 完成,但由于您已经使用 vb.net 进行标记,也许您需要 .NET 解决方案。另外,由于您没有提供代码,我将弥补一些

Public Class Data
    Public Property Description As String
    Public Property Quantity As Integer
End Class
Dim datas = New List(Of Data)()
datas.Add(New Data With {.Description = "Phone X", .Quantity = 10})
datas.Add(New Data With {.Description = "AB Laptop B", .Quantity = 20})
datas.Add(New Data With {.Description = "X Phone", .Quantity = 15})
datas.Add(New Data With {.Description = "Laptop C", .Quantity = 20})
datas.Add(New Data With {.Description = "AB Phone X", .Quantity = 5})

Dim groupedRecords = datas.
    GroupBy(Function(r) If(r.Description.ToUpper().Contains("PHONE"), "Phone", "Laptop")).
    Select(Function(g) New With {.Description = g.Key, .Quantity = g.Sum(Function(i) i.Quantity)})

For Each record In groupedRecords
    Debug.Print($"Description: {record.Description}, Quantity: {record.Quantity}")
Next

描述:电话,数量:30

描述:笔记本电脑,数量:40

如果您添加更多产品,这将会中断。它将描述中带有“电话”的任何东西识别为电话,将其他任何东西识别为笔记本电脑。

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

Public Class Data
    Public Property Description As String
    Public Property Quantity As Integer
End Class
Dim datas = New List(Of Data)()
datas.Add(New Data With {.Description = "Phone X", .Quantity = 10})
datas.Add(New Data With {.Description = "AB Laptop B", .Quantity = 20})
datas.Add(New Data With {.Description = "X Phone", .Quantity = 15})
datas.Add(New Data With {.Description = "Laptop C", .Quantity = 20})
datas.Add(New Data With {.Description = "AB Phone X", .Quantity = 5})

Dim groupedRecords = datas.
    GroupBy(Function(r) If(r.Description.ToUpper().Contains("PHONE"), "Phone", "Laptop")).
    Select(Function(g) New With {.Description = g.Key, .Quantity = g.Sum(Function(i) i.Quantity)})

For Each record In groupedRecords
    Debug.Print(
quot;Description: {record.Description}, Quantity: {record.Quantity}")
Next

Description: Phone, Quantity: 30

Description: Laptop, Quantity: 40

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.

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