sql - 如何在 Google 表格中使用 SQL 查询按子字符串进行分组

标签 sql google-sheets group-by substring google-sheets-query

在我的 Google 表格表格中,我有一个名为“description”的 B 列,我正在尝试编写一个 SQL 查询来按 B 列的子字符串进行分组。B 中的值类似于“Sell 1 Jan11 300.0/307.5” Strangle”,我只想按“Jan11”分组。我有几行包含 Jan11,然后切换到 Jan18,依此类推。

我尝试过子字符串、字符索引、mid,但没有任何效果 我试过:

=QUERY('spgsh1378'!A1:AP,"select B, mid(B,7,5), sum(M) group by mid(B,7,5)"

=QUERY('spgsh1378'!A1:AP,"select B, substring(B,7,5), sum(M) group by substring(B,7,5)")

=QUERY('spgsh1378'!A1:AP,"select B, substring(B, 2, CHARINDEX(' ', B) - 2), sum(M) group by substring(B, 2, CHARINDEX(' ', B) - 2)"

我收到一个解析错误,内容为

"Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "(" "( "" at line 1, column 14. Was expecting one of: "where" ... "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "," ... "" ... "+" ... "-" ... "/" ... "%" ... "" ... "/" ... "%" ... "+" ... "-" ..."

最佳答案

您的尝试不是有效的语法。尝试这样:

=ARRAYFORMULA(QUERY({MID(spgsh1378!B1:B, 7, 5), spgsh1378!A1:AP}, 
 "select Col3,Col1,sum(Col14) 
  where Col1 is not null 
  group by Col1,Col3
  label sum(Col14)''", 0))

=ARRAYFORMULA(QUERY({QUERY({MID('grouping project'!A1:A, 8, 5), 'grouping project'!A1:AO}, 
 "select Col1,sum(Col3) 
  where Col1 is not null 
  group by Col1
  label Col1'month',sum(Col3)'net'",1), 
 IF(LEN(QUERY(QUERY({MID('grouping project'!A1:A, 8, 5), 'grouping project'!A1:AO}, 
 "select Col1,sum(Col3) 
  where Col1 is not null 
  group by Col1
  label Col1'month',sum(Col3)'net'",1),"select Col1",0)), {"";MONTH(LEFT(
 QUERY(QUERY({MID('grouping project'!A1:A, 8, 5), 'grouping project'!A1:AO}, 
 "select Col1,sum(Col3) 
  where Col1 is not null 
  group by Col1
  label Col1'month',sum(Col3)'net'",1),"select Col1 offset 1", 0), 3)&1)&RIGHT(
 QUERY(QUERY({MID('grouping project'!A1:A, 8, 5), 'grouping project'!A1:AO}, 
 "select Col1,sum(Col3) 
  where Col1 is not null 
  group by Col1
  label Col1'month',sum(Col3)'net'",1),"select Col1 offset 1", 0), 2)}, )*1}, 
 "select Col1,Col2 order by Col3"))

0

关于sql - 如何在 Google 表格中使用 SQL 查询按子字符串进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57364636/

相关文章:

google-apps-script - 如何在谷歌表格中创建函数 sleep(毫秒) 以延迟或暂停一定范围内的公式

regex - 如何在谷歌表格公式中提取精确的 10 位 ISBN10

python - 在 Python 列中查找最大值

php - 选择数据并在所选记录中查找重复值的sql查询

sql - 在 Postgres 中从另一个物化 View 调用一个物化 View

mysql - 更新查询不更新几行

sql - 从表中选择多列,但按一列分组

mysql - MySQL 使用什么风格的 SQL?

validation - 如何在Google表格中的数据验证规则中仅显示过滤后的数据范围

Scala 集合 : array groupBy and return array indexes for each group