Query data in SheetA where column matches SheetB - google-sheets

I have 2 sheets, SheetA and SheetB. The Rows in each sheet are unsorted but there is a 1:1 map of Columns A on each sheet. I am trying to do something like sql, where i say:
=select B from SheetA where SheetA.name == SheetB.A2
SHEETA
name, age
foo, 20
gary, 30
--------
SHEETB
name, hobby, age
gary, kites, =select B from SheetA where SheetA.name == A2
foo, boats, ...
I was trying the query but that returns lists, which actually would be what i wanted, but the data is not expected to be sorted.

Like some LOOKUP? You can try with:
=XLOOKUP(A2,'Sheet B'!A2:A,'Sheet B'!B2:B ,,0)
And, if you want for the whole column'
=INDEX(XLOOKUP(A2:A,'Sheet B'!A2:A,'Sheet B'!B2:B ,,0))

Related

Formula to build report of employees that are not working between 2 dates

I am trying to find a way to see which employees are not working between 2 dates.
Should I use vlookup, index & match, filter, query, or something else?
Sheet 1 contains employee details & start/end dates.
Sheet 2 accepts user input to select 2 dates, and it will automatically display a list of available employees who are not working.
Sheet 1 - Database/Log of all employees and days worked.
#
A
B
C
D
1
ID
Name
Start Date
End Date
2
12345
John
01/01/2021
01/08/2021
3
54321
Sarah
01/24/2021
01/29/2021
4
00731
James
02/05/2021
02/15/2021
5
00731
John
02/10/2021
02/30/2021
Sheet 2 (Row 1-2)- Manually enter in two dates.
#
A
B
1
Start Date (Manual input)
End Date (Manual input)
2
01/01/2021
01/30/2021
Sheet 2 (Row 3+)- List of all employees that are not working between the two dates entered in Sheet 2!A2:B2 (Expected Results)
#
A
B
3
ID
Name
4
00731
James
try:
=INDEX(SUBSTITUTE(UNIQUE(QUERY(""&SPLIT(FLATTEN(IF(SEQUENCE(1, MAX(D2:D-C2:C))<=D2:D-C2:C,
"♥"&A2:A&"♦"&B2:B&"♦"&C2:C+SEQUENCE(1, MAX(D2:D-C2:C), 0), )), "♦"),
"select Col1,Col2 where not Col3 matches '"&JOIN("|", "^$",
IF(SEQUENCE(1, G2-F2)<=G2-F2, F2+SEQUENCE(1, G2-F2, 0), ))&"'", 0)), "♥", ))
demo sheet
Assuming the name of your first sheet with the full data is actually Sheet1, place the following in Sheet2 cell A4:
=FILTER(Sheet1!A2:B,Sheet1!A2:A<>"",(Sheet1!C2:C>G2)+(Sheet1!D2:D<F2))
The combined either/or condition (Sheet1!C2:C>G2)+(Sheet1!D2:D<F2) in the FILTER means "either the start date is after the range date given, or the end date is before the range date given."
The other condition of Sheet1!A2:A<>"" just rules out blank rows in the original data set. It's not strictly necessary as far as the visual results are concerned; but it keeps null rows from being added to those results, which would allow you to enter data below the results in Col A and B of Sheet2 if you wanted, or to have fewer rows in Sheet2 than in Sheet1 without the formula adding more rows to accommodate null returns.
Your posted sample data is unclear. You have two different names for the same ID (i.e., 00731). And you have an "End Date" in D5 of February 30, 2021—which is not a valid date. In any case, it's unclear whether the same person/ID may turn up twice in the original data set. My formula above assumes you will not.
If your original data list may, in fact, contain duplicates, things get a bit trickier. In that case, use the following formula instead, in Sheet2 cell A4:
=UNIQUE(FILTER(A2:B,ISERROR(VLOOKUP(A2:A,FILTER(A2:A,((C2:C>=F2)*(C2:C<=G2))+((D2:D>=F2)*(D2:D<=G2))),1,FALSE))))
Here, the inner FILTER first forms a list of all people who are working during that range, and then the outer FILTER filters in a UNIQUE set of the people who are not on that inner list (i.e., trying to VLOOKUP them returns IS(an)ERROR).

Google Sheets query function needing to provide 0 value for no value found

The query function looks at a separate sheet with orders entered by a dealer. In some months the dealer may not have a order. The B:B column in current sheet is all the dealers in Ascending order. Then I have separate columns for by month view with 3 columns per month. Number of contracts, amount then average. My query calculates correctly, but if a dealer doesn't have a contract then it skips. So my list is out of order. I'm needing it to place a 0 if no value found. I have 2 versions of the query.
J = amount, H = Dealer name, A = Date, the B = is the dealer list in current sheet.
This query populates but out of order due to skipping NUll or NA.
=QUERY('2021ContractsData'!A:V,
"Select COUNT(J),SUM(J),AVG(J)
WHERE MONTH(A)+1=1
Group By H LABEL COUNT(J) 'Contracts',SUM(J) 'Amount',AVG(J) 'Average'")
This query populates nothing, it shows the Header names but no values in rows.
=QUERY('2021ContractsData'!A:V,
"Select COUNT(J),SUM(J),AVG(J)
WHERE MONTH(A)+1=1
AND H='"&B2:B&"'
Group By H LABEL COUNT(J) 'Contracts',SUM(J) 'Amount',AVG(J) 'Average'")
Google Sheet View
try in C2:
={"Contracts", "Amount", "Average";
INDEX(IFNA(VLOOKUP(B3:B, QUERY('2021ContractsData'!A:V,
"select H,count(J),sum(J),avg(J)
where month(A)+1=1
group by H"), {2,3,4}, 0), 0))}

Filter on reduced values from a column?

I'm sorry this is titled so horribly.
I have a category column in a sheet (col H) and in another sheet I've got the unique values in those columns with unique(sheetname!H)
How do I get only the categories (col H values) whose value in, say, col F, is 'credit'?
A given category will always have a given col-F value: will always be credit or debit. And will always belong to a certain person, as well. Say the person's name is in column B. How do I display the person who "belongs" to that category?
Using queries should make this simple.
Something like this should work:
=QUERY(A:H, "select H where F='credit'",0)
More details on how the information is stored would help.
The first parameter is the range of cells to perform the query on. The second parameter is the query. The third is the number of header rows at the top of data.
More info on QUERY: https://support.google.com/docs/answer/3093343?hl=en
To display the names in column B along with the categories, you can use:
=QUERY(A:H, "select B,H where F='credit'",0)
try:
={H1; FILTER(H2:H; F2:F="credit")}
for unique:
={H1; UNIQUE(FILTER(H2:H; F2:F="credit"))}

Google Sheets - Trouble with Query and Array

I'm struggling with the following and help/guidance would be appreciated. The attached Google Sheet has 3 sheets;
Sheet1 has 2 data fields (month, store name).
Sheet2 has 4 data fields (month, store name, fruit, quantity)
A query on Sheet3 in cell A3 outputs a set of months and store names from Sheet1 which is then used to find quantities of a given fruit from Sheet2 (in this example, it's apples). The results are listed on Sheet3 in columns D and E. I used both Index(Match) and Filter to output the Apple quantities as i tried to figure out my ultimate goal - how to use a single formula, including the query itself, to get to the aggregate apple total in Row 9 (i.e. without needing to do all the index(match) or filter formulas). Said another way, what formula on Sheet3 in cell D9 would run the query against Sheet1, use the results to find the month, store name, fruit, and quantity matches on Sheet2, and total them for a single output cell on Sheet3 D9?
Thoughts?
You can use a combination of counta(query()) to do this.
For A1:A that contains a mixture of fruit (apples, bananas, grapes, star fruit), use:
=counta((query(A1:A, "select A where A like 'apple'")))
Check out this very simple example sheet

Look up value by "submission number" return "name"

I have one sheet, with values such as this:
Sheet A
and another sheet with values like so:
Sheet B
What I would like to do is search in the Sheet A by "Submission Number", i.e. the values in column A, I want to search there using the values in column B of Sheet B and return the Name of the company, the value in Sheet A, column B.
I was trying something like this but it didn't work:
=LOOKUP(B2, eligible.submissions!B2:B160,1,1)
So in the example above, for row 1 in Sheet B I want to use the value 724, i.e. (Sheet B, column B, row 1), to look in the Column A of Sheet A, there I would find that 724 corresponds to "Advice Business Development", and accordingly I want my function to return me that value, the name.
Is this possible with Google Sheets?
Which function to use?
I was also trying something like this:
=LOOKUP(B2, SheetA!B2:B160,SheetA!B2)
A Vlookup should do what you want.
VLOOKUP(B1;SheetA!A:B;2;FALSE)
This would be in A1 of SheetB
do it like this:
=LOOKUP(B2, eligible.submissions!A1:A158,eligible.submissions!B1:B158)

Resources