【问题标题】:How to aggregate string by specific ID - (String1 - a)(String1 - b)(String2 - a)(String2 - b)如何按特定 ID 聚合字符串 - (String1 - a)(String1 - b)(String2 - a)(String2 - b)
【发布时间】:2019-09-03 17:56:29
【问题描述】:

我需要能够根据字符串值及其标识符值聚合字符串和另一个单元格的值。

我尝试了几种对值求和并按特定项目过滤的方法,然后尝试根据包含的文本求和,但我没有找到好的方法。

╔══════════════╦═════════════════╗
║ A            ║ B               ║
╠══════════════╬═════════════════╣
║ Product Name ║ Product Quantity║
╠══════════════╬═════════════════╣
║ ProductA - 1 ║ 12              ║
╠══════════════╬═════════════════╣
║ ProductA - 2 ║ 2               ║
╠══════════════╬═════════════════╣
║ ProductB - 1 ║ 28              ║
╠══════════════╬═════════════════╣
║ ProductB - 2 ║ 21              ║
╠══════════════╬═════════════════╣
║ ProductC - 1 ║ 1               ║
╠══════════════╬═════════════════╣
║ ProductC - 2 ║ 4               ║
╠══════════════╬═════════════════╣
║ ProductD - 1 ║ 98              ║
╠══════════════╬═════════════════╣
║ ProductD - 2 ║ 75              ║
╠══════════════╬═════════════════╣
║ ProductE - 1 ║ 5               ║
╠══════════════╬═════════════════╣
║ ProductE - 2 ║ 9               ║
╚══════════════╩═════════════════╝

我不确定如何处理 sum(A) 和 sum(B),其中 A 包含除标识符之外的相同文本。

目前,我正在执行查询以从较大的数据集中仅获取 A 和 B,然后执行 Find and Replace 以删除所有 - 1- 2,然后执行

=QUERY(A:B,"Select A, sum(B) group by A order by sum(B) desc")```

【问题讨论】:

    标签: regex google-sheets array-formulas google-sheets-query google-query-language


    【解决方案1】:
    =ARRAYFORMULA(QUERY({IFERROR(REGEXEXTRACT(A2:A, "(.+) - \d+")), B2:B}, 
     "select Col1,sum(Col2) 
      where Col1 is not null 
      group by Col1 
      order by sum(Col2) desc
      label sum(Col2)''", 0))
    

    【讨论】:

    • 你能解释一下正则表达式吗?
    • 当然。我们在-之前正则提取所有(.+),然后是数字\d+
    • 有没有办法添加到这个?我有另一个产品表,分为(50 包)和(25 包)?这对它也非常有用。
    • 它将是:"(.+) \d+ Pack",假设项目是:xxx 50 Packyyy 25 Pack
    • 它将是:"(.+) \(\d+ Pack\)" 假设项目是:xxx (50 Pack),yyy (25 Pack)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2014-01-08
    • 1970-01-01
    • 2011-02-04
    相关资源
    最近更新 更多