在另一个键范围内查找一个键范围以获取所有对应的值,而不仅仅是第一个

LOOKUP a key range in another key range to get ALL corresponding values, instead of just the first

我想获取 Range1 中与 Range2 中的键对应的所有数据。 (如下图)

但是我的公式无法正确显示数据:

=ARRAYFORMULA(TRANSPOSE(QUERY(Range1,"select Col2 where Col1 contains '"&A1:A&"'",0)))

谁能帮忙告诉我公式中的问题?

范围 1

Type Desc
beverage coke
food cookies
food bread
beverage beer
beverage coffee
food chips

Range2(理想结果)

ColA(pre-set) ColB(arrayformula in B1) ColC ColD
food cookies bread chips
beverage coke beer coffee
food cookies bread chips
food cookies bread chips
beverage coke beer coffee
beverage coke beer coffee

我的存在结果

ColA(pre-set) ColB(arrayformula in B1) ColC ColD
food cookies bread chips
beverage (cannot generate the output)
food
food
beverage
beverage

假设您的源数据在 A 列和 B 列中,并且预期结果的最大列宽为 4,尝试:

=arrayformula(if(len(A2:A), vlookup(A2:A, regexreplace({unique(A2:A),  split(trim(transpose(query(if((transpose(unique(A2:A))=A2:A)*len(A2:A),B2:B&",",),,50000))), ",")}, ",$", ), {1, 2, 3, 4}, 0),))

看看是否有帮助?

=ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(QUERY(IF(TRANSPOSE(A12:A17)=A2:A7,B2:B7&"",),,2^9)),"")))

大小:95 个字符

解释:

  • 创建一个二维数组,比较范围 1(A2:A7) 与转置范围 2(A2:A17)

    =ARRAYFORMULA(TRANSPOSE(A12:A17)=A2:A7)
    
Range 1 v Range2 -> food beverage food food beverage beverage
beverage FALSE TRUE FALSE FALSE TRUE TRUE
food TRUE FALSE TRUE TRUE FALSE FALSE
food TRUE FALSE TRUE TRUE FALSE FALSE
beverage FALSE TRUE FALSE FALSE TRUE TRUE
beverage FALSE TRUE FALSE FALSE TRUE TRUE
food TRUE FALSE TRUE TRUE FALSE FALSE
  • 将 TRUE 更改为 Range1 说明 (B2:B7) 和分隔符 </code>:</p> <pre><code>=ARRAYFORMULA(IF(TRANSPOSE(A12:A17)=A2:A7,B2:B7&"",))

Range 1 v Range2 -> food beverage food food beverage beverage
beverage coke coke coke
food cookies cookies cookies
food bread bread bread
beverage beer beer beer
beverage coffee coffee coffee
food chips chips chips
  • 如果您在每一列中垂直查看,您将获得每个所需 header 的所有所需值。此时,我们使用经典查询来做垂直连接:

    =ARRAYFORMULA(QUERY(IF(TRANSPOSE(A12:A17)=A2:A7,B2:B7&"",),,2^9))
    
Range 1 v Range2 -> food beverage food food beverage beverage
beverage cookies bread chips coke beer coffee cookies bread chips cookies bread chips coke beer coffee coke beer coffee
food
food
beverage
beverage
food
  • 现在,我们只需按分隔符进行 TRANSPOSE 和 SPLIT:

    =ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(QUERY(IF(TRANSPOSE(A12:A17)=A2:A7,B2:B7&"",),,2^9)),"")))
    
Range 2 v - - - - - -
food cookies bread chips
beverage coke beer coffee
food cookies bread chips
food cookies bread chips
beverage coke beer coffee
beverage coke beer coffee