Google电子表格:在一个工作表中堆叠多个查询时跳过空查询

时间:2018-01-31 05:29:41

标签: mysql google-sheets

背景 我正在构建一个电子表格查询复合体,它自动返回所有每周事件。这些活动按地区排序,但并非所有地区都会每周举办活动。我使用分号从每个区域堆叠查询,例如{= QUERY(); QUERY(); QUERY(); QUERY()},每个都按区域名称标记。 查询复合体只需返回包含实际事件的查询。

问题 由于每周都不一定会在所有地区都有一个事件,我最终会在查询复合体中出现一个或多个空查询。这将导致我的查询仅返回带有其标签的查询,或者,在删除查询标签时,会导致查询复杂返回错误。

示例

={

Query(Data!A:O,"Select A,B,C,E,F where D = 'Phantom Row For Titles' order by D asc, C asc label B 'City'",1);

Query(Data!A:O,"Select A,B,C,E,F where D = 'Africa' order by D asc, C asc label C 'Africa'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'Asia' order by D asc, C asc label C 'Asia'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'Central America' order by D asc, C asc label C 'Central America'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'Europe' order by D asc, C asc label C 'Europe'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'Middle East' order by D asc, C asc label C 'Middle East'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'North America' order by D asc, C asc label C 'North America'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'Oceania' order by D asc, C asc label C 'Oceania'",0);

Query(Data!A:O,"Select A,B,C,E,F where D = 'South America' order by D asc, C asc label C 'South America'",0)

}

这是一个有效的电子表格示例,说明了我拥有的和我需要的东西: https://docs.google.com/spreadsheets/d/1VnDyxOdw9aJMJpIs7zKSdG3c6fUo-QxDU82zaw-k7Kk/edit?usp=sharing

2 个答案:

答案 0 :(得分:1)

我认为,没有直接的方法可以做到这一点。我建议在这里找到解决方法。

请尝试:

=QUERY(

{Query(Data!A:O,"Select A,B,C,E,F where D = 'Phantom Row For Titles' order by D asc, C asc label B 'City'",1);

IFERROR({"","","Africa","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'Africa' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","Asia","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'Asia' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","Central America","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'Central America' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","Europe","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'Europe' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","Middle East","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'Middle East' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","North America","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'North America' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","Oceania","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'Oceania' order by D asc, C asc",0)},{"x","x","x","x","x"});

IFERROR({"","","South America","","";Query(Data!A:O,"Select A,B,C,E,F where D = 'South America' order by D asc, C asc",0)},{"x","x","x","x","x"})},

"select * where Col2 <> 'x'",1)

我的基本逻辑是使用查询2次:

  1. 检查输出
  2. 如果输出超过2行,则返回结果。
  3. 这种方式很糟糕,因为它使公式加倍。

    然后我想到了不同的方法:

    • 如果没有标签且没有结果,查询将返回错误
    • 所以我使用了iferror函数,并在发生错误时给出了假行{"x","x","x","x","x"}
    • 最后一个查询是跳过“x”行。

答案 1 :(得分:0)

由于Google表格中的某些区域设置(法语设置)不允许使用数组文字{“ x”,“ x”,“ x”}(应该使用1行3列的数组“ x”作为值)用作一系列QUERY公式中的IFERROR值。 我使用公式“ ARRAY_CONSTRAIN”在IFERROR公式中生成了一个包含1行3列的数组。

为此,您将使用: = { IFERROR(QUERY('sheet1'!A2:C;“选择A,B,C,其中(A!='')”); ARRAY_CONSTRAIN(I1:K1; 1; 3)); IFERROR(QUERY('sheet2'!A2:C;“选择A,B,C,其中(A!='')”); ARRAY_CONSTRAIN(I1:K1; 1; 3)) }

因此,如果两个查询之一未能返回数据,则将改为插入I1:K1的值,从而避免所有公式失败。