In google sheets, I need to query a subset of a table.
The table range is A1:K, where column A is used for filtering.
I want to construct a query that looks like:
=query(A1:K,"Select B,C,D,E,F,G,H,I,J,K where A =x")
except I want it to look more like:
=query(A1:K,"Select B-K where A =x")
Is this possible?
In my case, I also need to be able to do this on averages, so:
=query(A1:K, "Select avg(B-K) where A=x label avg(B-K) ''")
The reason why I want to not spell this out is because this query is being generated dynamically. Is this possible, or must I generate the string for the query seperately?
I think, the only way is to generate the string.
But if you make query like this:
=query({A1:K}, "Select ...")
then A-Z notation becomes Col1, Col2, Col3 and so on. You can take advantage of it:
use this formula to generate text "Col2, Col3, Col4, Col5, Col6, Col7, Col8, Col9, Col10, Col11":
=join(", ",ARRAYFORMULA("Col" & row(OFFSET(A2,,,10))))
And this formula will make text "AVG(Col2), AVG(Col3), AVG(Col4), AVG(Col5), AVG(Col6), AVG(Col7), AVG(Col8), AVG(Col9), AVG(Col10), AVG(Col11)":
=join(", ",ARRAYFORMULA("AVG(Col" & row(OFFSET(A2,,,10))&")") )
Also change A2 and 10 in 2 formulas above to return different number of subsets.
Related
I am working on a project to extract a subset of a linked sheet using IMPORTRANGE and QUERY (using CONTAINS as a filter). These are musical works by composers from historically underrepresented backgrounds, so I have to filter by a demographic code (hidden) and a genre (shown).
My current method's results are mainly good, but some entries that don't match my criteria (i.e., genre-type) are trickling through (link below). Each tab of the spreadsheet should contain one genre from the original list (e.g., Orchestra, Concert Band, Jazz Ensemble), but at least four entries from other genres appear, regardless of the sheet's coded genre.
Is there a way to adjust my QUERY to prevent this bleed-through, or should I rewrite the formula using FILTER or another similar command?
Link to sheet: https://docs.google.com/spreadsheets/d/1ViXyrF5aaBCLp0izs5kLJpol77o-JZF4WYeD2C8DXTs/edit?usp=sharing
the problem is in your last "or" clause. try adding brackets to group your or statements together:
=query(importrange("1NxLcOH13Dxmg0VQMWQtjj_eJ38xyQ-BDV9J-t77hCDM", "New Works!A1:M"), "SELECT Col2, Col4, Col6, Col7, Col8, Col9, Col10, Col11, Col12 WHERE Col12 CONTAINS 'Orchestra' AND ((Col3 > 1 and Col3 < 6) or (Col5 > 1 and Col5 < 6)) ORDER by Col12, Col7, Col2 asc", 1)
I want to rebuild my table of Col1 (key), Col2, Col3, Col4, Col5 to table of Col1 (Key), and Col2 (which include all accordance values from Columns 2,3,4,5...), please check example:
https://docs.google.com/spreadsheets/d/1je3uc1DHitzzsFK6Xag_ld531p7ozeheO4PwpCfZjQA
If I use the following plain text as range in Query:
=QUERY({A2:A7\B2:B7;A2:A7\C2:C7;A2:A7\D2:D7;A2:A7\E2:E7},"Select * where Col2 >0")
it works well. But I can't make that range to be dynamic inside query, I tried to put range (based on formulas or even as a text) in some cell and then pull it as follows
=QUERY(A1,"Select * where Col >0")
or
=QUERY({indirect(A1)},"Select * where Col2 >0")
but it doesn't work.
You can use this formula where you will need to change only one range in case there will be more columns:
=SORT(SPLIT(QUERY(FLATTEN(IF(B3:E = "",, A3:A & "♥" & B3:E)), "WHERE Col1 IS NOT NULL"), "♥"))
Or you could place A3:A and B3:E ranges in some cells as parameters and use INDIRECT you you need that:
=SORT(SPLIT(QUERY(FLATTEN(IF(INDIRECT(N4) = "",, INDIRECT(N3) & "♥" & INDIRECT(N4))), "WHERE Col1 IS NOT NULL"), "♥"))
When a raw data set may expand, I always recommend that the raw data set remain by itself in a sheet. For now, let's assume that you have only your existing raw data set in the 'Sample' sheet.
In a new blank sheet, place this formula:
=ArrayFormula(QUERY(SPLIT(FLATTEN(FILTER(Sample!A3:A,Sample!A3:A<>"")&"|"&FILTER(FILTER(INDIRECT("Sample!B3:"&ROWS(Sample!A:A)),Sample!A3:A<>""),Sample!B2:2<>"")),"|"),"Select * WHERE Col2 Is Not Null",0))
This formula is similar to what #kishkin offered. However, the FILTER inclusions automatically expand to include all parts of the raw data where there is something in Column A and a header added in B3:B. That is, this formula requires zero maintenance if set up as I've described.
When I import data, it comes in this format (image 1), with blank spaces. I would like to know if there is any way to adjust so that these blanks disappear, the two models expected (image 2 and 3) if there was any way to reach them would be important to me.
Remembering that all dates have / and all times have :
I tried to filter from QUERY, but when trying to "Select Col1, Col2, Col4 Where Col2 is not null" the dates disappear and only the times remain, I tried via REGEXMATCH to separate the dates from the times using / and : but also I was not successful.
I also tried it via IMPORTXML, but some data ends up not being imported correctly on some pages of the site, for IMPORTHTML these errors do not happen. The XML's I used were:
"//tr[#class='no-date-repetition-new' and ..//td[#class='team team-a']] | //tr[#class='no-date-repetition-new live-now' and ..//td[#class='team team-a']]"
"//td[#class='team team-a']/a | //td[#class='team team-a strong']/a"
The current formula is as follows:
=IMPORTHTML("https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/","table",1)
IMPORTHTML Original:
Expected formats:
---
Rather than filtering what you need is to restructure the imported data.
Anyway, I think that the easier solution to get the final result is to use multiple IMPORTXML formulas.
URL
A1: https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/
Headers
A2: //table[contains(#class,'matches')]/thead/tr/th
Day
A3: //td[contains(#class,'date')]/parent::tr
Teams and Score
A4: //td[contains(#class,'team-a')]/parent::tr
A6: =transpose(IMPORTXML($A$1,A2))
A7: =IMPORTXML($A$1,A3)
B7: =IMPORTXML(A1,A4)
You might want to replace the formula on A6 by static values in order to place them properly.
You can join 2 queries together (one next to the other) in a single formula, to get your results
={QUERY(IMPORTHTML("https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/","table",1),
"select Col1 where Col2 is null and not Col1 contains '*'",1),
QUERY(IMPORTHTML("https://int.soccerway.com/national/austria/1-liga/20192020/regular-season/r54328/","table",1),
"select Col1, Col2, Col3, Col4 where Col2 is not null label Col1 'Time'",1)}
How the formula works:
As you notice the data part of both queries is the same in both of them. What is actually different is "what we ask for from the query"
In the first one we use "select Col1 where Col2 is null and not Col1 contains '*'"
In the second one "select Col1, Col2, Col3, Col4 where Col2 is not null label Col1 'Time'"
We create an array by joining them together as in ={1stQUERY,2ndQUERY}
I'm new to Sheets and the query function as of last night, so don't know the terminology etc - sorry!
I have two workbooks, a master and a template. I need the templete to pull specific data from the master when Col2 matches a particular number. I have it working great if i manually input the number, but don't want to have to do this everytime I give a copy of the template to a new job. This number in Col2 is unique to each of the templates I give to a new job, and the number will already be manually input into cell J16 of the template. I want the code to pull the number from J16 and search rather than me telling it what the number is everytime. The code I have that works with the manual input number, in this case 4032 is below. Thanks in advance for any help.
Vince.
=QUERY(IMPORTRANGE("1yTp3SajbKovHf_Wjx7CHO9FsIPeQGvmdMytQIkr9gog","Invoices"),"select Col1, Col5, Col11 where Col2 = 4032")
Do you want to get the number (any) from J16 into your query?
Use:
=QUERY(IMPORTRANGE("file_id","Invoices"),"select Col1, Col5, Col11 where Col2 = " & J16)
or better:
=QUERY(IMPORTRANGE("file_id","Invoices"),a1)
where a1 is:
="select Col1, Col5, Col11 where Col2 = " & J16
I am using this formula to import select columns from one Google Sheet to another:
=QUERY(IMPORTRANGE("1Y06zLsfEBPL8txxc6V0K6544iNjnNDGRYcKHtdixrOg","Reporting!A:S"),"select Col1, Col3, Col4, Col6, Col7, Col19")
Does anyone know why the values in the First and Second row of the columns are being merged automatically?
Ex: If A1 = "Socks" and A2 = "Hats" in the parent Google Sheet, then cell A1 in the imported range Google Sheet would be = "Socks Hats". Other than the first two rows, everything else looks good.
I think yor problem is data with empty cells, query formula can't automatically detect the header of your table. Try this:
=QUERY(IMPORTRANGE("1Y06zLsfEBPL8txxc6V0K6544iNjnNDGRYcKHtdixrOg","Reporting!A:S"),"select Col1, Col3, Col4, Col6, Col7, Col19", 1)