简体   繁体   中英

Google Sheets: Merge two tables (arrays) with many to many relationship

I have 2 tables (arrays) in Google Sheets, that they have a related column . And I need to join these tables. What I need is get a result being that the two tables have a many-to-many relationship.

Example sheet

具有多对多关系的表

In this example, I have two tables (A:C and E:G). I need a result like table I:N.

Table A:C -> I have players and how much them has completed certain game. (key: 'player'+'game')

Table E:G -> I have groups that each one has different goals for some games. (key: 'group'+'game')

I need a result that column 'game' is the related column of each table.

Note that :
row 15 of first table: ( d - b6 - 55% ), and
row 7 of second table: ( k7 - b7 - 60% ),
have no relation, so they aren't on third table.

I've tried to combine some formulas like ARRAYFORMULA , VLOOKUP , FILTER , INDEX , OFFSET ... But I hadn't success with none of them.


I've found a solution with script SUPERSQL() Function, but I wouldn't like to use scripts, just formulas.

Main idea is to place one key column vertically as it is, and another one horizontally ( TRANSPOSE() ).

Outside QUERY is just for rearranging columns and sorting.

More on TRANSPOSE(QUERY(TRANSPOSE(...))) and such is here .

Solution:

=QUERY(
  {
    A1:C1\ E1:G1;
    ARRAYFORMULA(
      SPLIT(
        REGEXREPLACE(
          QUERY(
            FLATTEN(
              IF(
                FILTER(B2:B; B2:B <> "") <> TRANSPOSE(FILTER(F2:F; F2:F <> ""));;
                    TRANSPOSE(QUERY(TRANSPOSE(FILTER(A2:C & "@"; B2:B <> ""));; COLUMNS(A2:C))) & " "
                  & QUERY(TRANSPOSE(FILTER(E2:G & "@"; F2:F <> ""));; COLUMNS(E2:G))
              )
            );
            "WHERE Col1 IS NOT NULL";
          );
          "@$";
        );
        "@ ";
      )
    )
  };
  "SELECT Col4, Col1, Col2, Col6, Col3
   ORDER BY Col4, Col1, Col2";
  1
)

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM