Google Sheets query Select column concat with Select Count column - google-sheets

I help manage loot for a group in an MMO. I've created a google sheet to maintain visibility into who needs what items. The sheet basically works the way i want, but i'm having some trouble with returning SELECT A concatenated with SELECT COUNT(A). Honestly, i'm not sure how to go about it, but this is what i've got:
Right now I'm transposing
SELECT A WHERE C CONTAINS <VALUE>
which returns like this for multiple results:
Name OtherName OtherName
Meanwhile, the values (but not the format) i want to return, i can get with :
SELECT A, COUNT(A) WHERE C CONTAINS <VALUE> GROUP BY A LABEL COUNT(A) ''"
which returns:
Name OtherName
1 2
The desired output is:
Name(1) Othername(2)
The sheet is here:
https://docs.google.com/spreadsheets/d/1HUcgVg2ZFHteXhRnk2W2j_9RU4RvMd7nMTqCuRgmUdo/edit?usp=sharing
And the queries in question are on Sheet1. I put some example queries I've tried also in the sheet.

try:
=INDEX(REGEXREPLACE(QUERY(TRANSPOSE(IFNA(IF(B11>0, QUERY(GearList!A:C,
"select A,count(A)
where C contains '"&$D11&"'
group by A
label count(A)''", 0)), )),,99^99), " (\d+)", "($1)"))

Related

Column LOOKUP Dynamic Query (Google Sheets)

Trying 'query' for the first time in google sheets. Need some assistance. Thanks in advance.
I have a google sheet with two tabs "sheet 1" and "sheet 2"
Sheet 1 Structure:
Sheet 2 Structure:
I am trying to create a dynamic query, where if Entity = Person "x" and Value = A or B or C, the Des number (from sheet2) is populated in Sheet 1 in yellow cells
So far I have tried these queries and for some reason it throws a "#N/A" error
QUERY({Sheet2!$A$1:$AC$10}, "SELECT Col1,Col"&MATCH(B1,Sheet2!$A$1:$AC$1,0))
LOOKUP($B$1,QUERY({Sheet2!$A$1:$AC$10}, "SELECT Col1, Col"&MATCH("L",Sheet2!$A$1:$AC$1,0)))
Can someone point me in right direction with this query?
It's bit more complex as you have first to unpivot Sheet2, then apply query, finally join the results in one cell.
=iferror(textjoin(char(10),,query(arrayformula(split(flatten(Sheet2!$A$2:$A$4&"|"&Sheet2!$B$1:$F$1&"|"&Sheet2!$B$2:$F$4),"|")),"select Col2 where Col1='"&$A2&"' and Col3='"&B$1&"' ")))
adjust Sheet2!$A$2:$A$4&"|"&Sheet2!$B$1:$F$1&"|"&Sheet2!$B$2:$F$4as necessary.
Explanation
Sheet2 is like
To unpivot Sheet2, try
=arrayformula(split(flatten(Sheet2!$A$2:$A$4&"|"&Sheet2!$B$1:$F$1&"|"&Sheet2!$B$2:$F$4),"|"))
then apply your query
query(_______________,"select Col2 where Col1='"&$A2&"' and Col3='"&B$1&"' ")
and finally join the results

GS Query to split a column (Last, First) into 2 columns

I have a 'Raw Invoice' tab which is an excel file copy/pasted directly over. I'm then trying to format the data in the 'Invoices' tab in that column order using a query. I need to be able to break out the Student Name into two separate columns, hopefully within the query itself. Preferably it would then change it so column C is Last Name and column D is First name and the rest of columns shift over one.
I don't know if there's a way to perform a SPLIT function within the query. Right now I'm using a clunky method by doing a VLOOKUP on the student ID to get the names from another tab (not included in the Sample GS cuz it's an importrange from a work file), but it then creates two separate queries. Ideally I can somehow split column C within one query, but am getting lost by nesting queries and arrays together. I might be able to use REGEXEXTRACT, but again get lost in where to put it in the query or whether that's overkill.
QUERY('Raw Invoice'!$A:$I,"Select F,B,A, 'Bobs Diner',D,G,I where C is not null label 'Bobs Diner' 'Company' Format F 'M/DD/YYYY' ",1)
Link to sheet.
split in query arguments is not possible but you can do:
=ARRAYFORMULA(QUERY({IFERROR(SPLIT('Raw Invoice'!A:A, ", ")), 'Raw Invoice'!B:I},
"select Col7,Col3,Col1,Col2,'Bobs Diner',Col5,Col8,Col10
where Col3 is not null
label 'Bobs Diner' 'Company'
format Col7 'M/DD/YYYY'", 1))
Wat I suggest is to implement in Row Invoice
=arrayformula(split(A3:A,","))
and then
=QUERY('Raw Invoice'!$A:$K,"Select F,B,J,K, 'Bobs Diner',D,G,I where C is not null label 'Bobs Diner' 'Company' Format F 'M/DD/YYYY' ",1)

Google Sheet Filter formula based on range of cells

I've got some data
Basically a list of Items
and another sheet that contains a list of orders
(Some items can appear in multiple orders, which is why I can't use a vlookup for this)
My problem is I want to get the ALl the Order IDs of all items in dynamic list(in my example there's only 3 items, but that can grow.
I'm trying to use the filter formula and have got this so far:
=filter('Orders'!AC1:AD,'Orders'!K:K=A4)
which works fine at retrieving all the Order ID's for the item number in cell A4.
But I want the Order ID's for all the Items in column A.
I tried
=filter('Orders'!AC1:AD,'Orders'!K:K=A2:A)
But that doesn't work. I'm guessing I need to do some kind of array formula maybe.
But I can't figure it out.
You can use the QUERY function
This is a SQL like syntax to manipulate your data.
=query({Orders!$A:$B},"select Col2 where Col1 matches '"&textjoin("|",true,unique(Summary!$A4:$A))&"' ",1)
or this if you need to sort the result:
=query({Orders!$A:$B},"select Col2 where Col1 matches '"&textjoin("|",true,unique(Summary!$A4:$A))&"' order by Col2 ",1)
The first argument is the range that you want to query. Which in this case is inserted with the array notation {Orders!$A:$B}.
The next argument is a string representing an SQL like statment that in this case says "Select column 2 when column 1 matches Item A or Item C or Item D".
The "Item A or Item C or Item D" part is constructed with another formula, TEXTJOIN. Just grabbing the range to join and the delimiter is set to the OR operator which is |.

Extract Value from Cell on Another Sheet Based on List

I have a list of data on one sheet. A second sheet is used to display who's done what, like a dashboard. I've been given an array formula to generate the data that looks up the employee's name, and displays it if they are present on the list for a course completed. If so, a "yes" is listed on the dashboard under the corresponding course number. I cannot figure out what I need to change the REGEXREPLACE with to present the date column from the data list instead of "yes". I'm aware REGEXREPLACE only works for text values, and dates aren't - even changing the date column to text seems to matter not.
Here is a working example of the current array formula:
https://docs.google.com/spreadsheets/d/1jkG515zyl4UxRHxhtFTjgWsjG0aBE4vsOxbpvqgjogU/edit#gid=536376041
Here is the formula used:
=ARRAYFORMULA(IF(A5:A="",,REGEXREPLACE(IFNA(VLOOKUP(A5:A,
QUERY({TRIM('Form Responses 1'!B2:G)}, "select Col1,count(Col1) group by Col1 pivot Col6"), MATCH(F2:P2,
QUERY(QUERY({TRIM('Form Responses 1'!B2:G)}, "select Col1,count(Col1) group by Col1 pivot Col6"), "limit 0", 1), 0), 0))&"", "\d+", "yes")))
In the above example, I need the Date Completed from col D on the Form Responses sheet.
Here is a first attempt that might help you.
This is a straightforward query, that pulls the dates, and pivots to have the employee names on the left, and the course names on the top.
But it doesn't try to match up data with an existing list of employees - it just lists all of the employees that have submitted a form. So if you want to see all employees, with blank rows for those who haven't submitted a form, this won't work for you.
The formula, in A2,is:
=ARRAYFORMULA(QUERY({'Form Responses 1'!B2:G}, "select Col1,max(Col3) where Col1<>'' group by Col1 pivot Col6 order by Col1"))
See tab Sheet1-GK, added to your sample sheet.
Let me know if this helps, or if you need something different.
UPDATE:
To limit the result to a specific list of courses, use the following modification:
=ARRAYFORMULA(QUERY({'Form Responses 1'!B2:G},
"select Col1,max(Col3) where Col1<>''
and Col6 matches '" & TEXTJOIN("|",1,F2:2) & "'
group by Col1 pivot Col6 order by Col1 "))
Here, the list of desired courses to report on is in F2:2, where you had them originally, but this list could be kept anywhere, even on another tab. If you name the range where you place it, that can simplify this formula a bit. For now, you could just hide row 2. I've grouped it, on the left, to hide it. Use the [+] to reveal it again.

Trying to include additional columns in QUERY result on GoogleSheets

I'm using Google Sheets and using the following formula without any issue:
=QUERY(Sheet!A:D,"select A, max(B) where not B is null group by A label A 'Client', max(B) 'Most Recent'")
Obviously, this is returning columns A and B of the referenced data
However, I would like to also return the corresponding cells from column C.
sample data: https://docs.google.com/spreadsheets/d/1CJKiDNPUWMMUbLLb-UDA6iRA2PBnlMHDsEB9vELe0Rk/edit?usp=sharing
Using this example, what I would like to see is cell G2 populated with "Pizza" (i.e., from C3), and so on.
Am I using the right formula? What do I need to change?
What you are trying to do is not very SQL-like, because max(B) does not point to any particular row: it's just the maximum value of B among the selected rows. This value could be attained by several rows, making the choice of C, D ambiguous. I don't think that a single query can do this.
Here is an implementation with unique and several query commands.
In E2, enter =unique(A:A) to get the list of unique client names.
In F2, enter
=query(A:D, "select B,C,D where A ='"&E2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so either add label B '', C '', D '' or just don't include the header row in the queried range.
Version with grouping by C,D
To also select C and D within a single query, expand the select clause
select A, max(B), C, D
which will then require including those in the group by clause:
group by A, C, D
The formula will be
=QUERY(A:D,"select A, max(B), C, D where not B is null group by A, C, D label A 'Client', max(B) 'Most Recent'")
This does mean that the only rows to be grouped together will be those where not only A but also C and D are equal.
A possible one-formula solution:
=ArrayFormula(IFERROR(VLOOKUP(UNIQUE(A2:A),SORT(A2:C,2,0),{1,2,3},0)))
Here is what I did that finally worked.
In E1, enter =unique(A:A) to get the list of unique client names. In F2, enter
=query(A$2:D101, "select B,C,D where A ='"&F2&"' order by B desc limit 1")
and drag this formula down. This selects all rows with the A value matching E2, and picks one with maximal B value.
You don't want to have a header row in the output of these queries so just add desired text in header row.

Resources