date comparision google sheet query - google-sheets

I am using Google-sheets.
I have a Data-table like:
x|A | B | C
1|date |randomNumber|
2| 20.02.2018 | 1243 |
3| 18.01.2018 | 2 |
4| 17.01.2018 | 1 |
and a overview table:
x|A | B | C
1|date |randomNumber|
2| 20.02.2018 | |
3| 17.01.2018 | |
I want to lookup the dates on the overview table and lookup their value in my data table. Not every date of the data sheet has to appear in the overview table. All colums are date-formatted.
My approach so far was:
=QUERY(Data ;"select B where A = date '"&TEXT(A2;"yyyy-mm-dd")&"'")
but i get an empty output, which should not be the case, I should get 1243.
Thanks already :)

=ARRAYFORMULA(VLOOKUP(A2:A3;DATA!A1:B4;2;0))

With QUERY (copied down from B2 to suit):
=QUERY(Data!A:C;"select B where A = date '"&TEXT(A2;"yyyy-mm-dd")&"'";0)

Related

How to make select with variable using Google Sheets

I have a Google sheet with several columns, where are recorded support requests from clients.
A B C
-+---------------+------------------------+-------------
1| Date-1 | John | Ticket-101
2| Date-1 | Anita | Ticket-102
3| Date-2 | John | Ticket-103
4| Date-3 | Dani | Ticket-104
5| Date-3 | Billy | Ticket-105
I want to create two new columns with statistical data about the clients. In these new columns, I want to have the client name and number of opened support tickets.
The end result must be:
A B C D E
-+---------------+------------+-------------+-----------+---------------
1| Date-1 | John | Ticket-101 | John | 2 |
2| Date-1 | Anita | Ticket-102 | Anita | 1 |
3| Date-2 | John | Ticket-103 | Dani | 1 |
4| Date-3 | Dani | Ticket-104 | Billy | 1 |
5| Date-3 | Billy | Ticket-105 |
I created the D column in this way:
=UNIQUE(QUERY(B1:B))
For counting how many times the client contact us I use:
=COUNTA(IFERROR(QUERY(B1:B, "select B where B='John'", 0)))
Of course, this is a very stupid solution, because for every new client I must to create a new formula with
....where B='Client name'".....
I'm wondering is it possible to create a formula in a way in which the name of the client is automatically populated? I imagine something like that:
=ARRAYFORMULA(COUNTA(IFERROR(QUERY(B1:B, "select B where B='value-of-D'", 0))))
=QUERY(B:C,
"select B,count(C)
where B!=''
group by B
label count(C)''", 0)
also you can order it like:
=QUERY(B:C,
"select B,count(C)
where B!=''
group by B
order by count(C) desc
label count(C)''", 0)
Try
=query(B:C, "Select B, count(C) where B<>'' group by B label B 'Name', count(C) 'Count'", 1)
and see if that works?

Partial transpose of Sheet

I have a Google Sheet with this format:
+---------+---------+---------+------------+------------+------------+------------+--------+--------+
| Field_A | Field_B | Field_C | 24/09/2019 | 25/09/2019 | 26/09/2019 | 27/09/2019 | day... | day... |
+---------+---------+---------+------------+------------+------------+------------+--------+--------+
| ValX | ValY | ValZ | Val1 | Val2 | Val3 | Val4 | | |
| ValW | ValY | ValZ | Val5 | Val6 | Val7 | Val8 | | |
+---------+---------+---------+------------+------------+------------+------------+--------+--------+
First 3 columns are specific fields and all other columns are related to one specific day in a given (and static) range.
I need to convert the table in the following format:
+---------+---------+---------+------------+-----------+
| Field_A | Field_B | Field_C | Date | DateValue |
+---------+---------+---------+------------+-----------+
| ValX | Valy | Valz | 24/09/2019 | Val1 |
| ValX | Valy | Valz | 25/09/2019 | Val2 |
| ValX | Valy | Valz | 26/09/2019 | Val3 |
| ... | | | | |
+---------+---------+---------+------------+-----------+
Basically, the first 3 columns are gathered as-is, but the day-column in transposed (is even the correct term?) with 2 values:
The date
The value in the cell related to date
Is something that can be achieved with formula or do I need to create a bounded AppsScript?
Following a sample Sheet demo: https://docs.google.com/spreadsheets/d/1cprzD96i-4NQ8tieA_nwd8s43yKF-M8Kww4yWNfB6tg/edit#gid=505040170
In Sheet Start you can see the initial data and format, 3 static columns and one column for every da
In Sheet End you can see the output format I'm looking for, the same 3 static columns, but the date and cell value related to date are transposed as a row.
You can see the Formula I used, TRANSPOSE for every row, where I select the days for the IV column and one row at a time for the V row.
For the 3 static columns, I replicated the Formula for every instance of the day related to that row.
This is working but requires much manual work to set up every single TRANSPOSE. I'm wondering if there is a more automatic way of doing this (except for using AppsScript, in that case, I'm already planning on doing this if not other solutions are available)
=ARRAYFORMULA(TRIM(SPLIT(TRANSPOSE(SPLIT(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(Start!D2:F<>""; "♦"&TRANSPOSE(QUERY(TRANSPOSE(Start!A2:C&"♠");;999^99))&
TEXT(Start!D1:F1; "dd/mm/yyyy")&"♠"&Start!D2:F; ));;999^99));;999^99); "♦")); "♠")))

Get whole row by two unique fields in a Google Sheet

I have tried to achieve this for the last few hours with no luck so I am appealing for help.
I have a google form which saves to a google spreadsheet and the responses are recording in a structure as per below.
---------------------------------------------------------
| WEEK | CATEGORY | COL1 | COL2 | TIMESTAMP |
|--------|------------|---------|----------|--------------|
| 1 | CAT1 | VALUE1 | VALUE1 | YYYY-MM-DD |
| 1 | CAT1 | VALUE2 | VALUE2 | YYYY-MM-DD |
| 1 | CAT2 | VALUE1 | VALUE1 | YYYY-MM-DD |
| 2 | CAT3 | VALUE1 | VALUE1 | YYYY-MM-DD |
| 2 | CAT3 | VALUE2 | VALUE2 | YYYY-MM-DD |
---------------------------------------------------------
I need a formula which I can use in google sheets to take the most recent entry where WEEK and CATEGORY together are unique values.
Eg using the above table, the formula would result in something that looks like this.
---------------------------------------------------------
| WEEK | CATEGORY | COL1 | COL2 | TIMESTAMP |
|--------|------------|---------|----------|--------------|
| 1 | CAT1 | VALUE2 | VALUE2 | YYYY-MM-DD |
| 1 | CAT2 | VALUE1 | VALUE1 | YYYY-MM-DD |
| 2 | CAT3 | VALUE2 | VALUE2 | YYYY-MM-DD |
---------------------------------------------------------
Any help would be greatly appreciated!
Thanks,
There isn't a super clean solution for what is basically a dynamic self-join in SQL parlance, but this might be manageable:
Get a list of maximum timestamps per category and week
Filter the original table by this derived list using a concatenated key
Any attempts to improve this answer appreciated (in particular the string concatenation as key is not very elegant).

Query completed with an empty output

https://docs.google.com/spreadsheets/d/1033hNIUutMjjdwiZZ40u59Q8DvxBXYr7pcWyRRHAdXk
That's a link to the file in which it is not working! If you open it, go to sheet named "My query stinks".
The sheet called deposits has data like this in columns A (date), B (description), and C (amount):
+---+-----------+-----------------+---------+
| | A | B | C |
+---+-----------+-----------------+---------+
| 1 | 6/29/2016 | 1000000044 | 480 |
| 2 | 6/24/2016 | 1000000045 | 359.61 |
| 3 | 8/8/2016 | 201631212301237 | 11.11 |
+---+-----------+-----------------+---------+
The sheet "My Query Stinks" has data in columns A (check number), B (failing query) and C (amount):
+---+-----------------+------+--------+
| | A | B | C |
+---+-----------------+------+--------+
| 1 | 1000000044 | #N/A | 480 |
| 2 | 1000000045 | #N/A | 359.61 |
| 3 | 201631212301237 | #N/A | 11.11 |
+---+-----------------+------+--------+
In Column B on My Query Stinks, I want to enter a query. Here's what I'm trying:
=query(Deposits!A:C,"select A where A =" & A2)
For some reason, it returns "#N/A Error Query completed with an empty output." I want it to find that 1000000044 (the value in C4) matches 1000000044 over on Deposits and return the date.
Try
=query(Deposits!A:C,"select A where B ='" &A2&"'")
Explanation
Values like 1000000044 in Column B of the Deposit sheet and Column A of My Query Stinks sheets are set as text (string) values, so they should be enclosed on single quotes (apostrophes) otherwise QUERY think this values are numbers or variable names.
Try this:
=query(Deposits!A:C,"select A where B = '"&A2&"' LIMIT 1")
You'll need LIMIT 1 as you have multiple deposits for the same value in your second column.
Another solution for this problem could be to replace '=' with 'contains':
=query(Deposits!A:C,"select A where B contains '" &A2&"'")
Simple, but this error cost me half a morning.

Outer Join using multiple Google Sheets and the QUERY function

As an example, say I have the following sheets in the same workbook of a Google Doc:
SHEET1 | SHEET2
\ A | B | \ A | B | C | D
1| ID |Lookup | 1| Lookup| Name |Flavor | Color
2| 123 | 4445 | 2| 1234 |Whizzer|Cherry | Red
3| 234 | 4445 | 3| 4445 |Fizzer |Lemon | Yellow
4| 124 | 1234 | 4| 9887 |Sizzle |Lime | Blue
5| 767 | 1234 |
6| 555 | 9887 |
Obviously, Google Docs isn't made with relational databases in mind, but I am trying to obtain results similar to the SQL query
SELECT
SHEET1.ID,
SHEET2.*
FROM
SHEET1
LEFT JOIN
SHEET2
ON SHEET1.Lookup = SHEET2.Lookup
resulting in a table that looks like
SHEET3
\ A | B | C | D | E
1| ID |Lookup | Name |Flavor | Color
2| 123 | 4445 |Fizzer |Lemon | Yellow
3| 234 | 4445 |Fizzer |Lemon | Yellow
4| 124 | 1234 |Whizzer|Cherry | Red
5| 767 | 1234 |Whizzer|Cherry | Red
6| 555 | 9887 |Sizzle |Lime | Blue
but this is where I stand currently
SHEET3
\ A | B | C | D | E
1| | | | |
2| 123 | 4445 | #N/A | |
3| 234 | 4445 | | |
4| 124 | 1234 | | |
5| 767 | 1234 | | |
6| 555 | 9887 | | |
At the moment I have managed to use the QUERY function to grab the values from SHEET1 and have tried a few different QUERY functions in SHEET3!C1 in an attempt to "LEFT JOIN" the two sheets using this blog post as a reference. At this point, the two functions I am using are as follows.
SHEET3!A2=QUERY(SHEET1!A2:B20, "SELECT A,B")
SHEET3!C2=QUERY(SHEET2!A2:E20, "SELECT B,C,D WHERE A="""&B2&"""")
and hovering over the error in C2 reads "Query completed with an empty output". How can I join these sheets?
Additional references:
Google Docs syntax page for QUERY
Do this in Sheet3.
In cell A1, to get the correct headings:
={Sheet1!A1:B1,Sheet2!B1:D1}
In cell A2, to get the table of Joined data, try this formula:
=FILTER({Sheet1!A2:B,
VLOOKUP(Sheet1!B2:B, {Sheet2!A2:A, Sheet2!B2:D}, {2,3,4}, false)},
Sheet1!B2:B<>"")
I've written a comprehensive guide about this topic called:
'Mastering Join-formulas in Google Sheets'
If you copy SHEET1 into SHEET3 (A1) then in C2:
=vlookup($B2,Sheet2!$A:$D,column()-1,0)
copied across and down should give the results you show once you have added three column labels.
Sheet3!A2:
=ARRAYFORMULA(Sheet1!A2:B20)
Sheet3!C2:
=ARRAYFORMULA(VLOOKUP(B2:B20,A1:D50,{2,3,4},0))
The following Add-on will provide all you need: Formulas by Top Contributors, by using both the build-in SQL join types and the MATRIX formula:
=MATRIX(SQLINNERJOIN(Sheet1!A1:B6,2,Sheet2!A1:D4,1, TRUE),,"3")
It will yield the following outcome:
I've created an example file for you: SQL JOINS
Affiliation: as a Google Top Contributor I helped creating the Add-on

Resources