Search code examples
google-sheetsgoogle-sheets-formulaarray-formulasgoogle-sheets-querygoogle-query-language

Google Sheets query where not working with cell reference


I use the query function to extract data from my tables in google sheets. But one of the conditions must be a reference to another cell. I do it like this:

=QUERY(Orig!A:Z;"select F where D= '"&C2&"' ";-1)

But it finds no coincidences. However, if I put the content of cell C2 directly in the query like this it works:

=QUERY(Orig!A:Z;"select F where D=3400 ";-1)

the data in D that I use for the select is numeric, and also the data in the reference cell C2. I tried casting to text, but nothing worked!


Solution

  • when the source (C2) is number your query needs to look like this:

    =QUERY(Orig!A:Z; "select F where D="&C2; -1)
    

    otherwise, you would need to do it like this:

    =ARRAYFORMULA(QUERY(TO_TEXT(Orig!A:Z); "select Col6 where Col4='"&TO_TEXT(C2)&"'"; -1))