Query with range of values for WHERE clause? - sql

I have a Google Spreadsheet and I want to run a QUERY function. But I want the WHERE statement to check a series of values. I'm basically looking for what I would use an IN statement in SQL - what the IN equivalent in Google Spreadsheets? So right now I have:
=QUERY(Sheet1!A3:AB50,"Select A,B, AB WHERE B='"& G4 &"'")
And that works. But what I really need is the equivalent of:
=QUERY(Sheet1!A3:AB50,"Select A,B, AB WHERE B='"& G4:G7 &"'")
And of course, that statement fails. How can I get the where against a range of values? These are text values, if that makes a difference.

Great trick Zolley! Here's a small improvement.
Instead of:
=CONCATENATE(G3,"|",G4,"|",G5,"|",G6,"|",G7)
we can use
=TEXTJOIN("|",1,G3:G7)
That also allows us to work with bigger arrays when adding every cell into the formula one by one just doesn't make sense.
UPD:
Going further I tried to compose two formulas together to exclude the helping cell and here we go:
=QUERY(Sheet1!A3:AB50,"Select A,B, AB WHERE B matches '^.(" & TEXTJOIN("|",1,G3:G7) & ").$'")
Used this in my own project and it worked perfectly!

Although I don't now the perfect answer for that, I could find a workaround for the problem, which can be used in small amount of data (hope that's the case here :) )
First step: You should create a "helper cell" in which you concatenate the G4:G7 cells with a "|" character:
=CONCATENATE(G3,"|",G4,"|",G5,"|",G6,"|",G7) - Let's say it's the content of the cell H2.
Now, you should change your above query as follows:
=QUERY(Sheet1!A3:AB50,"Select A,B, AB WHERE B matches '^.*(" & H2 & ").*$'")
This should do the trick. Basically we the "matches" operator allows the use of regular expressions, which also allow construction using the | symbol.
As I said, it's a workaround, it has drawbacks, but I hope it'll help.

I had the same question and came across your question in my research. Your Concatenate idea got me thinking. The problem is that the range that I'm using in my where clause is dynamic. The number of rows changes. That got me onto the Join function which lead me to this:
=(QUERY('Sheet1!A3:AB50,"select A where B = "& JOIN(" OR B = ",ARRAY_CONSTRAIN($A$5:A,COUNTIF($G$4:$G,">0"),1)) &"")
COUNTIF counts the number of rows with data. I'm using numbers in this range so maybe "!= ''" would be more appropriate than ">0". ARRAY_CONSTRAIN creates an array with with cells that only has data. JOIN turns the range into query language for the where clause.
Need to start where clause with basically same text that is in delimeter in the JOIN function. Note that I am using numbers so I don’t need ‘’ around values. This is what works in my spreadsheet and I hope it helps.

Related

What is the difference between Concat and Concatenate?

As I am learning data analysis, the words concat and concatenate came up in some of my lessons. I understand how to use them, but I cannot tell what makes the two different.
in google sheets (as you tagged your question) there are:
CONCAT can join only two things while CONCATENATE can join two or more things.
however, the best you can do is to know they exist and never ever use them. they are totally obsolete and with arrays you will face more pain with both of them than expected.
if you want to join stuff in google sheets use
JOIN
but more superior is textjoin because it is able to skip blank cells
TEXTJOIN
tho, the best way to join stuff is to use good old & especially when it comes to arrays. it's short, always works and it's short. example:
=ARRAYFORMULA(A2:A10&"-"&B2:B10)
wanna it shorter (?) drop the arrayformula and use index:
=INDEX(A2:A10&"-"&B2:B10)
wanna join A2 with C3 and D3:
=A2&C3&D3
wanna add delimiter:
=A2&"-"&C3&"-"&D3
or:
=JOIN("-"; A2; C3; D3)
or some other example:
=TEXTJOIN("-"; 1; A2:A5; G10)
also, it's worth noting that all of them (CONCAT, CONCATENATE, JOIN, TEXTJOIN) have a limit of 50000 characters.
to join larger datasets you will need to use QUERY like:
=QUERY(A1:A;;9^9)

How do I code Google query variables referring to text with an apostrophe in it?

Although I can find solutions to errors caused when a simple text value in a Google query contains an apostrophe, I cannot yet find a solution to when a variable in a Google query might (or might not) refer to a text value that contains an apostrophe.
I am using a simple code to extract a value (column B) related to a list of schools (column A), some of which have apostrophes in their names.
query(SchoolsData_db,"select B where A = '"&$A13&"' label B ''")
For all of the schools with apostrophes I get a #VALUE! error.
I am wondering if there is code I can run against the variable itself ('"&$A13&"') to handle the possible apostrophe. I know I can create a "cleaned name" column corollary to the school names, and then a reverse lookup to restore the names with my results table, but I am hoping the problem can be solved in each calculation instead.
You might try creating a variable first and concatenating that to the query string.
OK - interpreting the suggestion I got, I created and included a text ID column in the schools data (ex: S0001, etc.) and then I used that ID as my variable in the query. All is well. Thanks. I hope others can find this if they are similarly perplexed.

Excel or Numbers, How Populate adjacent Column?

So I feel like this is a pretty simple question, but I cannot for the life of my find the answer, here or elsewhere.
I'm trying to autopopulate a column with custom text. I suppose it would be the row adjacent.
Thought vlookup was the solution, but I'm rusty.
Basically it's financial, if the Description contains, say, "Amazon" or "Subway" I'd like to populate the adjacent cell with "Amazon" or "Online Shopping" or "Subway" or Fast food.
I'm using numbers but assume that excel advice would apply for such a simple (seemingly) task.
Make sense?
Also, hope I formatted the image correctly.
Ok thanks!
Just looking at the sample data I can see a pattern that emerges from these transactions. However, My first thought would be to jump to VBA for Excel but I don't believe that is available for Mac OS.
Vlookup will only work with the Range_Lookup set to TRUE which means it will try to find the closest match. This might lead to incorrect matches returned or problems with the requirement for sorting your table array that is being queried.
The only other thing that came to mind which would work for a single query value such as "Amazon" OR "Subway" would be to use a nested formula that checks if that substring is found in the Description column for each cell. This would be something like:
=IF(FIND("Amazon",D1)>0,"Amazon","")
The problem with this is that it only checks for one value and it does not have an error handling mechanism so each string that is checked without the word "Amazon" in it will return a #Value error in Excel.

IF and conditional SUM in Google Doc Spreadsheet

I am trying to run an IF statement in Google Spreadsheet that will, if "Yes" SUM a series of values.
=IF(G3="Yes",=SUM(C3*D3)+(E3*D3))
This works (if I ignore the IF) and just do =SUM(C3*D3)+(E3*D3), so I know my math is correct.
I have read a few different posts that are asking similar questions, but many have "guesses" and are offering different structured formulas, so I'm not really even sure what the proper structure is any more.
Basically, for the nerdy portion of you, the spreadsheet does the following:
If the "killed" column is Yes, I need to calculate the XP of the monsters killed.
Base XP (C3) times Qty (D3), plus Bonus XP (E3) times Qty (D3) and them SUM the value.
The equal sign (=) in front of SUM should not be there. If your formula works I guess Google Docs just ignores it. Also, the function SUM() is useful to add the values from a range of cells from the same row or column when you don't know in advance how many cells you will add (or there are more than 2 cells and you use SUM() because it's less to write).
If I understood your request, the formula you need is:
=IF(G3="Yes",C3*D3+E3*D3)
Right now the SUM function is only wrapping C3*D3 then you are adding (E3*D3). This is the same as (C3*D3)+(E3*D3) not using the SUM function. Order of operations tells us there is no need for the parentheses so you could write C3*D3+E3*D3.
The IF function has the following parameters:
IF(EVALUATION,IF TRUE,IF FALSE)
So your final equation would be:
=IF(G3="YES",C3*D3+E3*D3,"")
I always add the FALSE return to be blank so that if I need to change it later I can do so.

Find and replace from spreadsheet into spreadsheet...(Hard to sum up, sorry)

I know the title isn't informative at all, but I had no idea how to sum up my question. I have 2 spreadsheets, one of them 400+ words paired with a number for each word and the other one has has the text that I will be using. So the first spreadsheet looks like this:
Column A Column B
afds 0
dshs 1
dhid 2
. .
. .
. .
dgsrs 456
And the second spreadsheet looks something like this:
Column A Column B
dhid afds
dshs dgsrs
etc. etc.
I would like to get the second spreadsheet to display the numbers that are assigned to the strings in the first spreadsheet. So I would eventually like to end up with a spreadsheet that looks like this:
Column A Column B
2 (used to be dhid) 0 (used to be afds)
1 (used to be dshs) 456 (used to be dgsrs
(The comments in brackets is just to explain, I don't want them in the final spreadsheet)
So I started doing find&replace (find a string in the second spreadsheet and replace it with the value that is assigned to it in the first spreadsheet), but I know that there must be a script for this. I am not extremely familiar with any language, but so far I have been able to run simple scripts in languages I hadn't seen before, so if you know any solution to this, please let me know, I don't care what programming language it is in, I can google a way to run it as long as it runs on mac os x (c compiler installed).
Thanks in advance!
While you could write a script to do this, the use of a translation table is common enough to be supported in many spreadsheet formula languages. In Excel-flavoured languages -- I'll use OpenOffice on the Mac, although this works with Numbers as well -- you can use the function VLOOKUP. Its calling syntax in OO looks like
VLOOKUP( valueToSearchFor ; tableToSearchIn ; columnIdentifier ; isTableSorted)
Probably easier to show it, so:
On the left hand side I put your translation table. In the centre were the cells to be translated. And on the right I wrote the formula VLOOKUP(D3; $A3:$B6; 2; 0), which translates into "look for the value of cell D3 in the translation table in the fixed region from A3-B6, use the second column as the values, and the table isn't sorted so don't try to take shortcuts while searching". Then I simply copied that cell from F3 to the F3:G4 region, to match the shape of the central data columns.
BTW, I can never remember which programs are happy with semicolons and which ones need commas, so you may need to change that.. Seaching for VLOOKUP and whatever program you use should give you all the tutorials you need. I think modern Excel even automates the process.