Search code examples
google-sheetsgoogle-sheets-formula

How to create a query where I can combine two tables on another sheet and add an extra column


Tables: The image of 2 tables

I used

=QUERY({Sheet1!A2:G6, ARRAYFORMULA("Company A") ; Sheet1!H2:N6, ARRAYFORMULA("Company B")}, "SELECT * WHERE Col2 IS NOT NULL") 

but it kept giving me an error.

The images will show the resultant table


Solution

  • EDIT (the formula works)
    In your test sheet (the formula canNOT be posted, the sheet is in view only) you should use:

    =QUERY({QUERY({Sheet1!A1:G}, 
               "select Col1, Col2, Col3, Col4, Col5, Col6, Col7, 'company A' Label 'company A' 'Company' ", 1);
            QUERY({Sheet1!I2:O}, 
               "select Col1, Col2, Col3, Col4, Col5, Col6, Col7, 'company B' Label 'company B' '' ")}, 
             "where Col1 is not null ")
    

    Original reply

    Try the following

    =QUERY({QUERY({H10:L14}, 
               "select Col1, Col2, Col3, Col4, Col5, 'company A' Label 'company A' 'Company' ", 1);
            QUERY({H16:L18}, 
               "select Col1, Col2, Col3, Col4, Col5, 'company B' Label 'company B' '' ")}, 
             "where Col1 is not null ")
    

    (Do adjust the formula according to your ranges and locale)

    enter image description here