如何根据 Google 表格中的数字和特定文本将多行数据合并为一行?

How to combine data from multiple rows into a single row based on numbers and certain texts in Google Sheets?

我正在为交易目的创建一个日志。在交易中,假设我以 1 美元买入 +1 股迪士尼,以 2 美元卖出 -1 股迪士尼。现在这笔交易将显示在两个不同的行中,一行代表以 1 美元的价格购买了 1 股迪士尼,下一行显示交易退出意味着 -1 股迪士尼以 2 美元的价格,现在我想合并来自交易入场行和交易出场行在一行中,以便在一行中同时显示入场价格和出场价格及其各自的时间。

我需要所需的行来显示不同行的入场时间、入场价格和出场时间以及出场价格。另外,多次入场或多次出场时,需要对时间和价格进行平均。

这对我来说有点复杂, 如果你们能提供帮助,我将不胜感激。

第一个table是原始数据

第二个 table 是所需的输出。

我不确定我是否理解你需要什么,但你不能只使用=average(C10:C11)。如果这就是您的意思,那么您可以对所有需要的平均值使用相同的公式。

举个例子:https://docs.google.com/spreadsheets/d/1-koCscNdEBUoBo4fcB9BoDy0RrfY3r4NpZwHoFSLpN8/edit#gid=0

查看并使用 query/pivot 后,我必须得到以下输出:

我必须准备源数据

  • 通过将时间格式化为数字
  • 以及数字的位置
  • 通过添加一列来标识某种 OrderID,以便 link 相互买卖活动,因此它可用于数据透视表中的分组。我这样做的方式并不完全正确,因为当您在全部卖出之前购买额外的商品时,就会出现问题。
  • 我还添加了一个列来标识它是买入还是卖出以用作数据源。它可能也可以在查询中完成。

另外要记住的另一件事是,当您一次卖出超过 1 件时,这可能应该在平均值中占更多的权重。所以实际上应该计算一个加权平均值,而不仅仅是平均值。

这是my sample sheet

这是查询

=query(DATA!A2:G, "select F, A, C, avg(B), avg(E), max(D)
  where A is not null
  group by F, A, C
  pivot G
  order by A
  label F 'OrderID',
        A 'Date',
        C 'Company',
        avg(B) 'Time',
        avg(E) 'Price',
        max(D)  'Size'
  format avg(B) 'HH:MM:SS'
")

初始答案: 要计算每天的平均退出时间,请使用:

=average(query(A2:E6,"Select A where year(A) = "&year(A11)&" and month(A) = "&month(A11)-1&" and day(A) = "&day(A11)&" and C starts with '-' and B matches '"&D11&"'"))

要计算每天的平均退出价格,请使用:

=average(query(A2:E6,"Select D where year(A) = "&year(A11)&" and month(A) = "&month(A11)-1&" and day(A) = "&day(A11)&" and C starts with '-' and B matches '"&D11&"'"))

其中 'A11' 是对您的摘要日期的引用,'D11' 是您对 company/stock 姓名

的引用

首先,让我们做一些修复...在数据样本中,您假设单元格 A11 中有错误,其中年份显示为 222 而不是 2022


下一步...理论上,以下公式可以工作,但不建议使用它,因为数据集越大,性能方面就会对电子表格造成影响。也就是说,对于您提供的数据样本,它有效:

={"Date","Entry Time","Exit Time","Company","Entry Price","Exit Price","Size";
 ARRAYFORMULA(QUERY(QUERY({A2:E, IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0))), 
 (IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), 
 IF(NOT(REGEXMATCH(TO_TEXT(D2:D), "-"))*COUNTIFS(A2:A&C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), A2:A&C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), ROW(X2:X), "<="&ROW(X2:X)), {B2:B, E2:E}, ),  
 (IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))<1)*REGEXMATCH(TO_TEXT(D2:D), "-"), 
 IF(REGEXMATCH(TO_TEXT(D2:D), "-")*COUNTIFS(A2:A&C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))<1)*REGEXMATCH(TO_TEXT(D2:D), "-"), A2:A&C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))<1)*REGEXMATCH(TO_TEXT(D2:D), "-"), ROW(X2:X), "<="&ROW(X2:X)), {B2:B, E2:E}, ), 
 A2:A&C2:C&VLOOKUP(ROW(X2:X), IF(
 IF(A2:A="",,IF(VLOOKUP(C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X), "<="&ROW(X2:X))-1, 
 {SPLIT(UNIQUE(C2:C)&"×0♦0", "♦"); C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X),"<="&ROW(X2:X)), IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))}, 2, )<>0,,
 IF(NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), COUNTIFS(C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), ROW(X2:X), "<="&ROW(X2:X)), )))<>"", {ROW(X2:X), 
 IF(A2:A="",,IF(VLOOKUP(C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X), "<="&ROW(X2:X))-1, 
 {SPLIT(UNIQUE(C2:C)&"×0♦0", "♦"); C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X), "<="&ROW(X2:X)), (IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-"))}, 2, )<>0,,
 IF(NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), COUNTIFS(C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), C2:C&(IFERROR(1*(IFERROR(ROW(A2:A)/0)&MMULT(1*TRANSPOSE(IF((TRANSPOSE(ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))>=ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>"")))))*(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>"")))=TRANSPOSE(  INDIRECT("C2:C"&MAX(ROW(A2:A)*(A2:A<>"")))&  INDIRECT("A2:A"&MAX(ROW(A2:A)*(A2:A<>""))))),   INDIRECT("D2:D"&MAX(ROW(A2:A)*(A2:A<>""))), 0)), ROW(  INDIRECT("X2:X"&MAX(ROW(A2:A)*(A2:A<>""))))^0)))>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), ROW(X2:X), "<="&ROW(X2:X)), )))}), 2)}, 
 "select Col1,avg(Col8),avg(Col11),Col3,avg(Col9),avg(Col12),count(Col11) 
  where Col1 is not null 
  group by Col1,Col3,Col13"), "offset 1", ))}


相反,最好使用一个辅助列(G列)和这个公式:

={"Date","Entry Time","Exit Time","Company","Entry Price","Exit Price","Size";
 ARRAYFORMULA(QUERY(QUERY({A2:E, G2:G, 
 (G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), 
 IF(NOT(REGEXMATCH(TO_TEXT(D2:D), "-"))*COUNTIFS(A2:A&C2:C&(G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), A2:A&C2:C&(G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), ROW(X2:X), "<="&ROW(X2:X)), {B2:B, E2:E}, ),  
 (G2:G<1)*REGEXMATCH(TO_TEXT(D2:D), "-"), 
 IF(REGEXMATCH(TO_TEXT(D2:D), "-")*COUNTIFS(A2:A&C2:C&(G2:G<1)*REGEXMATCH(TO_TEXT(D2:D), "-"), A2:A&C2:C&(G2:G<1)*REGEXMATCH(TO_TEXT(D2:D), "-"), ROW(X2:X), "<="&ROW(X2:X)), {B2:B, E2:E}, ), 
 A2:A&C2:C&VLOOKUP(ROW(X2:X), IF(
 IF(A2:A="",,IF(VLOOKUP(C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X), "<="&ROW(X2:X))-1, 
 {SPLIT(UNIQUE(C2:C)&"×0♦0", "♦"); C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X),"<="&ROW(X2:X)), G2:G}, 2, )<>0,,
 IF(NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), COUNTIFS(C2:C&(G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), C2:C&(G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), ROW(X2:X), "<="&ROW(X2:X)), )))<>"", {ROW(X2:X), 
 IF(A2:A="",,IF(VLOOKUP(C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X), "<="&ROW(X2:X))-1, 
 {SPLIT(UNIQUE(C2:C)&"×0♦0", "♦"); C2:C&"×"&COUNTIFS(C2:C, C2:C, ROW(X2:X), "<="&ROW(X2:X)), (G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-"))}, 2, )<>0,,
 IF(NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), COUNTIFS(C2:C&(G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), C2:C&(G2:G>0)*NOT(REGEXMATCH(TO_TEXT(D2:D), "-")), ROW(X2:X), "<="&ROW(X2:X)), )))}), 2)}, 
 "select Col1,avg(Col8),avg(Col11),Col3,avg(Col9),avg(Col12),count(Col11) 
  where Col1 is not null 
  group by Col1,Col3,Col13"), "offset 1", ))}


它的重置分组功能(第 N 列 - Erik Tyler helped me with it )花费了一段时间,但就其简单性而言,如果您有正确的输入,它是一个平庸的解决方案:

=QUERY(A2:N, "select A,avg(I),avg(L),C,avg(J),avg(M),count(L) 
              where A is not null 
              group by A,C,N")


demo spreadsheet