How can I query importrange where Col values = any of the range specified in another sheet?
For example:
=QUERY(IMPORTRANGE("/18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA/","Data!A2:E"),"select Col4,Col3,Col1,Col2 where Col4 = '"&Helper!A2:A&"'",0)
This workbook has 3 sheets in it:
Data! = Data source
Helper! = Range required for the query to search for
Testing! = Is where I would like the data to return (Testing!A2 has the formula I have tried but it is not working as expected)
https://docs.google.com/spreadsheets/d/18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA/edit?usp=sharing
I would like it to return data for the range Helper!A2:A but it is currently only returning the data from Helper!A2 I'm not sure what is going wrong as no errors return.
=QUERY(IMPORTRANGE("/18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA/","Data!A2:E"),"select Col4,Col3,Col1,Col2 where Col4 matches '"&TEXTJOIN("|",TRUE,Helper!A2:A)&"'",0)
use:
=QUERY(IMPORTRANGE("18UF6ZR19iWulTMHT3lg6mv0NLrghItzW6bRT_p7bzsA","Data!A2:E"),
"select Col4,Col3,Col1,Col2
where Col4 matches '"&TEXTJOIN("|", 1, Helper!A2:A)&"'", 0)
Related
I've used query a lot of times to filter rows, like select * WHERE R = "ColContas" in this case filtering rows and selecting only the ones where R has the "ColContas" tag.
This time I need to filter columns, I just want to select the columns where in line 5 there is "ColContas" or "ColNum". That is to say, this:
Should turn into this:
Should be something like select * WHERE “row5” = “ColContas” OR “row5” = “ColNum”
How could I do that?
try:
=TRANSPOSE(QUERY(TRANSPOSE(A5:G); "where Col1 matches 'ColContas|ColNum'"; 1))
I've the below formula using ImportRange and Query along with Join and Split working correctly:
=join(" / ", QUERY(IMPORTRANGE("Google-Sheet-ID","RawData!A:AC"),"select Col25 where Col1 = " & JOIN(" OR Col1 = ", split(V2:V,"+")), 0))
Also, I've the below ArrayFormula with Split function working smoothly:
=ARRAYFORMULA(if(len(V2:V)=0,,split(V2:V,"+")))
But When I tried combining them together using the below formula:
=ARRAYFORMULA(if(len(V2:V)=0,,join(" / ", QUERY(IMPORTRANGE("Google-Sheet-ID","RawData!A:AC"),"select Col25 where Col1 = " & JOIN(" OR Col1 = ", split(V2:V,"+")), 0))))
It failed, and gave me the below error:
Error
Function SPLIT parameter 1 value should be non-empty.
Here is my sheet for your testing.
UPDATE
I changed it to:
=ARRAYFORMULA(if(len(C2:C)=0,,JOIN(" OR Col1 = ", ARRAYFORMULA(if(len(C2:C)=0,,split(C2:C,"+"))))))
So my full formula is:
=ARRAYFORMULA(
if(
len(C2:C)=0,,
join(" / ",
QUERY(
IMPORTRANGE("14iNSavtvjRU0XipPWIMKyHNwXTA85P_CafFTsIPHI6c","RawData!A:AC"),"select Col25 where Col1 = " &
ARRAYFORMULA(
if(len(C2:C)=0,,
JOIN(" OR Col1 = ",
ARRAYFORMULA(
if(
len(C2:C)=0,,split(C2:C,"+")
)
)
)
)
),
0
))))
And now getting the error:
Error
JOIN range must be a single row or a single column.
I believe this formula on the tab called MK.Testing will pull the info you're hoping for.
=QUERY(IMPORTRANGE("14iNSavtvjRU0XipPWIMKyHNwXTA85P_CafFTsIPHI6c","RawData!A:AC"),"select Col25 where Col1="&TEXTJOIN(" or Col1=",TRUE,A2:A))
I think you might have been overcomplicating things? This formula just forms a text string out of the shipment IDs to use in a query. one thing that may be tripping you up is that query() is very particular about the type of data in a column. Your shipment IDs can be numbers, or they can be number letter combos, but not both. That is, if you have some shipment IDs that contain letters and others that don't, it will be more difficult to get a query that works. (though not impossible). For the sake of helping you though, it's important that your sample IDs reflect the real ones in this way as accurately as possible.
How about doing this with Apps Script? You can get the values from the Sheet2, Shipment Ids, and the Ids from MK.Testing and compare them. If they coincide, the you copy the ETA into the Column C of MK. Testing:
function myFunction() {
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = sprsheet.getSheetByName("Sheet2");
var mkTesting = sprsheet.getSheetByName("MK.Testing");
var shipmentId = sheet2.getRange("A2:A").getValues();
var idList = mkTesting.getRange("A2:A").getValues();
for (var i = 0; i < shipmentId.length; i++){
for (var j = 0; j < idList.length; j++){
if (idList[j][0] == ""){break;} //Stops if there is an empty cell in Mk.Testing's column A
if (idList[j][0] === shipmentId[i][0]){
var eta = sheet2.getRange("E"+(i+2)).getValue();
mkTesting.getRange("C"+(j+2)).setValue(eta);
}
}
}
}
References:
SpreadsheetApp Class
Range Class
When I use the Query function like this using a number, it returns the correct results:
=QUERY(Sheet2!A1:B12,"select B where A matches '1200'", 0)
But, when I try to match a string, the result is always an empty output:
=QUERY(Sheet2!A1:B12,"select B where A matches 'qwerty'", 0)
This seems ultra-simple, but I can't seem to find an answer anywhere.. What exactly is the correct syntax to match the string? I've included an example of my problem here: sheets_query
I suggest you format ColumnA of Sheet2 as Plain text.
This indeed is simple, instead of matches, you need to use an "=" symbol. So your formula will be =QUERY(Sheet2!A1:B12,"select B where A='qwerty'", 0)
for number it would be:
=QUERY(Sheet2!A1:B12, "select B where A = 1200", 0)
for text, it would be:
=QUERY(Sheet2!A1:B12, "select B where A = 'qwerty'", 0)
for number with external reference:
=QUERY(Sheet2!A1:B12, "select B where A ="&1200, 0)
for text with external reference:
=QUERY(Sheet2!A1:B12, "select B where A = '"&"qwerty"&"'", 0)
for mixed data you can either format it as Plain text or:
=ARRAYFORMULA(QUERY(TO_TEXT(Sheet2!A1:B12), "select Col2 where Col1 = 'qwerty'", 0))
I have a column with a bunch of ingredients lists in it. I'm trying to figure out how many times different individual ingredients appear. There are 73,000 rows. The answers on this question works for a small amount of data in Google Sheets.
Formula is =UNIQUE(TRANSPOSE(SPLIT(JOIN(", ";A2:A);", ";FALSE)))
But I've overwhelmed JOIN with more than 50000 characters here. Is there another way to tackle this?
Sheet: https://docs.google.com/spreadsheets/d/1t0P9hMmVpwhI2IbATmIMjobuALTg8VWhl8-AQaq3zIo/edit?usp=sharing
=ARRAYFORMULA(UNIQUE(TRIM(TRANSPOSE(SPLIT(TRANSPOSE(
QUERY(","&A1:A,,5000000)),",")))))
=QUERY(QUERY(ARRAYFORMULA(TRIM(TRANSPOSE(SPLIT(TRANSPOSE(
QUERY(","&A1:A,,5000000)),",")))),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''"),
"order by Col2 desc")
demo spreadsheet
=UNIQUE(TRANSPOSE(SPLIT(REGEXREPLACE(TRANSPOSE(
QUERY(ARRAYFORMULA(","&A1:A),,5000000))," ,",","),",")))
but maybe you need this (?):
=QUERY(TRANSPOSE(SPLIT(REGEXREPLACE(TRANSPOSE(
QUERY(ARRAYFORMULA(","&A1:A),,5000000))," ,",","),",")),
"select Col1,count(Col1)
where Col1 is not null
group by Col1
label count(Col1)''")
I did a google scripting solution because I wanted to play with key map pairs.
function myFunction() {
var myMap = {"candy":0};
var sh = SpreadsheetApp.getActiveSpreadsheet();
var ss = sh.getSheetByName("FIRSTSHEETNAME");
var os = sh.getSheetByName("Ingredients");
var data = ss.getDataRange().getValues();
for (var i=0; i<data.length;i++)//full
//for (var i=1; i<4000;i++)//test
{
var array = data[i][0].split( ",");
for (var j=0; j<array.length;j++)
{
var item = array[j];
//Logger.log(array[j]);
if (myMap[item]>-1){
//Logger.log("REPEAT INGREDIENT");
var num = parseInt(myMap[item]);
num++;
myMap[item]=num;
//Logger.log(item +" "+num);
} else {
myMap[item]=1;
//Logger.log("New Ingredient: "+item);
//Logger.log(myMap);
}
}
}
//Logger.log(myMap);
var output=[];
for (var key in myMap){
//Logger.log("Ack");
output.push([key,myMap[key]]);
}
//Logger.log(output);
os.getRange(2,1,output.length,output[0].length).setValues(output);
}
You'll need to add an "Ingredients" tab for the output and change your first tab to be called FIRSTSHEETNAME (or change the code). In my testing it took 4 seconds for 4 items, 5 seconds for 400 items, and 6 seconds for 4000 items. there might be an issue with leading spaces but this gives you a place to start.
A fast running formula that works with columns of at least 40,000 rows:
=query(arrayformula(TRIM(flatten(split(A2:A20000,",")))),"select Col1,Count(Col1) Where NOT (Col1='' OR Col1 contains '#VALUE!') Group By Col1 order by Count(Col1) desc label Col1 'Ingredient',Count(Col1) 'Freq.'")
FLATTEN function, combined with SQL (QUERY function) can be a solution for fast filtering of values (such as empty or error messages).
TRIM function avoids artifacts in the result due to meaningless spaces before/after each string.
Sheet: https://docs.google.com/spreadsheets/d/1m9EvhQB1Leg2H7L52WhPe66_jRrTc8VsnZcliQsxJ7s/edit?usp=sharing
*In case of false case differences, you could normalize all characters of the strings to uppercase before within the same formula with UPPER(A2:A20000).
I have a google sheet with 3 tabs. I want to have tab3 pull all rows from tab1 (southware) into tab3 if there is a match to any value on tab2 (Top)column B
=query(southware!B3:AA,"SELECT * WHERE I = '"&Top!B3&"' and D='112'", 0)
I can get it to pull all the rows for only 1 cell on tab2- how do I make it evaluate all the values in column B on tab2
See if this helps
=query(southware!B3:AA,"SELECT * WHERE I matches '"&TEXTJOIN("|", 1, Top!B3:B)&"' and D='112'", 0)
QUERY is not able to work with arrays that's why you will need to use FILTER:
=FILTER(southware!B3:AA, I3:I = Top!B3:B,
D3:D = "112")
but this will may have few limitations, so in such case use this one:
=QUERY(ARRAYFORMULA(VLOOKUP(Top!B3:B,
{southware!I3:I, southware!B3:AA},
{2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27}, 0)),
"where Col3='112'", 0)