從其他行的組合創建行 (Create rows from combination of other rows)


問題描述

從其他行的組合創建行 (Create rows from combination of other rows)

我正在尋找一種基於兩個不同表重複記錄/行的方法,如下所示:

表 1:

Col1  Col2
A     101
B     102
C     103

表 2:

Col3
1
2
3

重複結果:

Col1   Col2   Col3
A      101    1
B      102    1
C      103    1 
A      101    2
B      102    2
C      103    2
A      101    3
B      102    3
C      103    3

在“Microsoft Access”或 SQL 中,我可以通過以下方式快速完成使用 SQL 選擇字符串:

SELECT table1.column1, table2.column1
FROM table1, table2 

但我想知道是否有任何方法可以在具有查詢功能等的谷歌表格中做到這一點。


參考解法

方法 1:

You can accomplish this via a custom function created in Google Apps Script. To achieve this, follow these steps:

  • In your spreadsheet, select Tools > Script editor to open a script bound to your file.
  • Copy this function in the script editor, and save the project:
function REPEAT(table1, table2) {
  var output = [];
  output.push([]);
  for (var i = 0; i < table1[0].length; i++) {
    output[0].push(table1[0][i]);
  }    
  for (var i = 0; i < table2[0].length; i++) {
    output[0].push(table2[0][i]);
  } 
  for (var i = 1; i < table2.length; i++) {
    for (var j = 1; j < table1.length; j++) {
      output.push([]);
      for (var k = 0; k < table1[j].length; k++) {
        output[output.length ‑ 1].push(table1[j][k]);
      }    
      for (var k = 0; k < table2[i].length; k++) {
        output[output.length ‑ 1].push(table2[i][k]);
      }       
    }
  }
  return output;
}
  • Now, if you go back to your spreadsheet, you can use this function just as you would do with any other function. You just have to provide the appropriate table ranges as parameters, as you can see here:

enter image description here

I hope this is of any help.

方法 2:

See if this works

=ArrayFormula(sort(split(transpose(split(textjoin(",", 1, split(textjoin(",", 1, A2:A4&"_"&transpose(B2:B4)),",")&"_"&A10:A12), ",")), "_")))

Change range to suit.

enter image description here

(by Jacob SpitzerIamblichusJPV)

參考文件

  1. Create rows from combination of other rows (CC BY‑SA 2.5/3.0/4.0)

#google-sheets-query #google-sheets






相關問題

IMPORTRANGE 將多個 Google 表格導入一個垂直列? (IMPORTRANGE to import multiple Google Sheets into one vertical column?)

如何使用度量單位對類似項目進行分組和添加 (How to group and add similar items with a unit of measurement)

如何在 Google 表格中創建多個工作表引用的 INDIRECT 數組字符串? (How to create INDIRECT array string of multiple sheet references in Google Sheets?)

如何匯總 Google 表格中列中的前 5 個值 (How can I sum top 5 values from column in Google Sheets)

使用查詢和過濾但“無列:Col15”錯誤 (Use Query and filter But "No Column: Col15" error)

如何從 Google 表格中的字符串中刪除一組數字 (how to remove a set of digits from string in Google Sheets)

如何在 Google 表格中使用 ArrayFormula 計算總數和唯一性(基於 2 個單獨的列)? (How to count totals and uniques (based on 2 separate columns) using ArrayFormula in Google Sheets?)

從其他行的組合創建行 (Create rows from combination of other rows)

將一對多數據轉換為列 (Transform one to many data to columns)

希望從右到左計算零值,直到出現非零值 (Looking to count zero values from right to left until non-zero values appears)

使用 ImportHTML 或 ImportXML 在 Google 表格中選擇不連續的列、刪除行和添加排序列 (Using ImportHTML or ImportXML to Select Non-Consecutive Columns In Google Sheets, Remove Rows, & Add A Sort Column)

使用 ARRAYFORMULA 從查找表中查找源表中的所有匹配行/值 (use ARRAYFORMULA to find all matching rows/values in a source table from a lookup table)







留言討論