Search code examples
google-sheetsgoogle-query-language

How to concatenate strings and select the same columns multiple times using Query (Google Sheets)


I am trying to generate a table for the Gantt chart. Table should have this format: https://developers.google.com/chart/interactive/docs/gallery/ganttchart#data-format

So,I need task name the same like taks ID, but in Query I can't use Col1 twice (I get error)

=QUERY({Tab1;Tab1};"select Col1,Col1,Col5,Col16,Col17 WHERE Col16>now() ORDER BY Col5 DESC,Col17 ";0)

The second point is that it is also not possible to merge two columns as a result, so it doesn't work:

=QUERY({Tab1;Tab1};"select Col1+Col7,Col1,Col5,Col16,Col17 WHERE Col16>now() ORDER BY Col5 DESC,Col17 ";0)

Here is my data and 2 results what I neet to get by QUERY https://docs.google.com/spreadsheets/d/1CZYgfYo6oIeONZOH6ZR5rOW615HuH4ICaoe7lj0dapw/edit#gid=0

These are such trivial things in a real SQL, is there no way to do it somehow straightforwardly in Google Query? So far I have found a combination of QUERY and ARRAYFORMULA but then there are very complicated queries - mutants. Not easier?


Solution

  • You don't need Query, just Arrays. You will get the first result from this code:

    ={ARRAYFORMULA(B3:B&" "&C3:C)\A3:A}
    

    The second result from this code:

    ={A3:A\A3:A\B3:B1}
    

    Based on your example I assume that you are not using US spreadsheet settings. If so formulas have to be change to:

    First:

    ={ARRAYFORMULA(B3:B&" "&C3:C),A3:A} 
    

    Second:

    ={A3:A,A3:A,B3:B}
    

    Link to working example: https://docs.google.com/spreadsheets/d/1eMkOkyFwvDeYSy-8UlhQum4OWcb-4WJqGxy_CXM8pVs/edit?usp=sharing


    I see that in your real sheet you would like to compare some data with now(). You can easily do this using array I propose as a source to Query. There will you have something like this (of course now it will not work - its only an example - an array have only 2 columns, not 15):

    =QUERY({ARRAYFORMULA(B3:B10&" "&C3:C10)\A3:A10};"select * where Col15>now()";0)
    

    About Query - you can't perform arthmetic operations on column containing strings. Look at the documentation: https://developers.google.com/chart/interactive/docs/querylanguage#arithmetic-operators