5

I have a master spreadsheet that contains columns like so:

FirstName   LastName   Gender   etc, etc, etc

I need to import from that master sheet into another sheet that filters by one of the other columns. In the new sheet, I need name to be a single column, joining FirstName and LastName together.

Here is the current query I am using:

=query(importrange($SPREADSHEET_KEY, "Full Overview!A2:AH"), "select Col1, Col2, Col5 where Col8 contains 'Migrators'", 0)

I need to merge Col1 and Col2 from the master sheet into Col1 of the new sheet and put Col5 in Col2 of the new sheet.

  • Do you know how to concatenate two columns in Google Sheets? – Rubén Aug 16 '16 at 14:22
  • Can you concatenate inside the select? I didn't think you could. – Jonathanb Aug 18 '16 at 9:28
  • Google Query select can concatenate but not in the way that you are expecting. See my answer. – Rubén Aug 18 '16 at 12:02
6

Short answer

QUERY select argument can't merge columns.

Explanation

The QUERY built-in function uses Google Visualization API Query Language. It doesn't include a concatenate operator. One alternative is to concatenate the data.

Examples

Assume that First Name and Last Name columns are columns A and B respectively.

Example 1

Add an auxiliary column to concatenate the desired columns in the source sheet and include this column in the IMPORTRANGE. Add one of the following formulas to an empty cell in the row 2:

  • =A2&" "&B2. Fill down as necessary.
  • =ARRAYFORMULA(A2:A&" "&B2:B) (Tip: Delete empty rows or use FILTER to only concatenate non-empty rows).

Example 2

Use several IMPORTRANGE, the concatenate operator & and arrays.

=ARRAYFORMULA(
  QUERY(
    {
      importrange($SPREADSHEET_KEY, "Full Overview!A2:A")&
      " "&
      importrange($SPREADSHEET_KEY, "Full Overview!B2:B")),
      importrange($SPREADSHEET_KEY, "Full Overview!C2:AH")
    },
    "select Col1, Col2, Col5 where Col8 contains 'Migrators'", 
    0
  )
) 
  • Perfect, did exactly what I needed. – Jonathanb Aug 23 '16 at 12:39
  • Please address the error in your Example 2, where you have an extra close-bracket ')'. – Richard Woolf Aug 6 at 5:21
4

In certain cases you can use Google array literal representation to achieve a join.

=query({Sheet1!A:B, Sheet2!C:D}, "select Col1, Col3 where Col2 = Col4")

This works only when the number of rows in both tables is same.

  • But select Col1, Col3 outputs 2 cols. How to combine them into one col? (Col1 & " " & Col3) – Pacerier Nov 13 '18 at 0:16
  • You can use JOIN statement on the result: =JOIN(" ", query({Sheet1!A:B, Sheet2!C:D}, "select Col1, Col3 where Col2 = Col4")) – rehan Nov 17 '18 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.