Maximum of column 1 where value of column 2 matches some condition - google-sheets

Let's say I have the following in a table :
A | B | desired_output
----------------------------
1 | 10 | 1 | 0
2 | 20 | 7 | 0
3 | 30 | 3 | 0
4 | 20 | 2 | 0
5 | 30 | 5 | 1
I'd like to find a formula for each of the cells in the desired_output column which looks at the max of B1:B5 but only for rows for which A = max(A1:A5)
If that's not clear, I'll try to put it another way :
for all the rows in A1:A5 that are equal to max(A1:A5) // so that's rows 3 and 5
find the one which has the max value on B // so between B3 and B5, that's B5
output 1 for this one, 0 for the other
I'd say there would be a where somewhere if such a function existed, something like = if(B=(max(B1:B5) where A = max(A1:A5)), 1, 0) but I can't find how to do it...
I can do it in two columns with a trick :
A | B | C | D
----------------------------
1 | 10 | 1 | | 0
2 | 20 | 7 | | 0
3 | 30 | 3 | 3 | 0
4 | 20 | 2 | | 0
5 | 30 | 5 | 5 | 1
With Cn = if(An=max(A$1:A$5),Bn,"") and Dn = if(Cn = max(C$1:C$5), 1, 0)
But I still can't find how to do it in one column

For systems without MAXIFS, put this in C1 and fill down.
=--(B1=MAX(INDEX(B$1:B$5-(A$1:A$5<>MAX(A$1:A$5))*1E+99, , )))

=ARRAYFORMULA(IF(LEN(A1:A), IF(IFERROR(VLOOKUP(CONCAT(A1:A&"×", B1:B),
JOIN("×", QUERY(A1:B, "order by A desc, B desc limit 1")), 1, 0), )<>"", 1, 0), ))
or shorter:
=ARRAYFORMULA(IF(A:A<>"",N(A:A&"×"&B:B=JOIN("×",SORTN(A:B,1,,1,0,2,0))),))
=ARRAYFORMULA(IF(A:A<>"",N(A:A&B:B=JOIN(,SORTN(A:B,1,,1,0,2,0))),))

How about the following:
=--AND(A5=MAX($A$1:$A$5),B5=MAXIFS($B$1:$B$5,$A$1:$A$5,MAX($A$1:$A$5)))

Related

Google SpreadSheet Query - Merge queries results into one

Let's take this data in a Google sheet:
| Product | Green | Red | Date |
| A | 1 | 0 | 1/1/2020 |
| A | 1 | 0 | 2/1/2020 |
| B | 0 | 1 | 2/25/2020 |
| C | 1 | 0 | 2/28/2020 |
| A | 0 | 1 | 3/1/2020 |
My goal would be to display the sum of Green / Red for each product:
From the beginning of the year,
For the current month.
I created this Google Query to get the results for all the year:
=QUERY(DATA!A:D,"select A, sum(B), sum(C) where D >= date '2020-01-01' and D <= date '2020-12-31' group by A")
I get this result:
| Product | sum Green | sum Red |
| A | 2 | 1 |
| B | 0 | 1 |
| C | 1 | 0 |
And this query for the given month (I simplified the query, but I have a Settings sheet to specify the month to query):
=QUERY(DATA!A:D,"select A, sum(B), sum(C) where D >= date '2020-01-01' and D <= date '2020-01-31' group by A")
And get this result:
| Product | sum Green | sum Red |
| A | 1 | 0 |
Now I'm stuck in joining the two results into one, like this:
| Product | Year sum Green | Year sum Red | Jan sum Green | Jan sum Red |
| A | 2 | 1 | 1 | 0 |
| B | 0 | 1 | | |
| C | 1 | 0 | | |
How can I achieve this ?
Thanks a lot for your help!
try:
=ARRAYFORMULA(IFNA(VLOOKUP(F2:F, QUERY(DATA!A:D,
"select A,sum(B),sum(C)
where month(D)+1 = 1
group by A
label sum(B)'Jan sum Green',sum(C)'Jan sum Red'"), {2,3}, 0)))

How to import data that has a value from other sheets

I have 2 spreadsheets and I'm trying to get all the items that the QTY is not equal to zero.
Can someone help me, please?
Example. If the items in Spreadsheet 1 is not 0 it will transfer to Spreadsheet 2 in one cell.
Spreadsheet 1
*---*----*---*----*
| | ITEMS | QTY|
*---*--------*----*
| 1 | BOOK | 4 |
| 2 | PEN | 4 |
| 3 | tape | 0 |
*---*----*--------*
Spreadsheet 2
+---+---------+
| | A |
+---+---------+
| 1 | BOOK 4 |
| | PEN 4 |
+---+---------+
This formula is the formula.
=JOIN(CHAR(10),QUERY(TRANSPOSE(IMPORTRANGE("URL","SUMMARY!B1:C3")),,2000000))
=TEXTJOIN(CHAR(10), 1, TRANSPOSE(QUERY(TRANSPOSE(QUERY(IMPORTRANGE(
"ID", "Sheet1!A:B"),
"where not Col2 = 0 and Col1 is not null", 0)), , 999^99)))

How to join next row in Cloud Spanner

How to join left next row in Cloud Spanner.
I want to calculate how many kilometers driven each driver.
My table looks like:
vehicle_id | driver_id | odometer
1 | 1 | 10
1 | 1 | 20
1 | 2 | 20
1 | 2 | 40
1 | 1 | 40
1 | 1 | 50
2 | 1 | 10
2 | 1 | 20
2 | 2 | 20
2 | 2 | 30
2 | 1 | 30
2 | 1 | 80
2 | 2 | 80
2 | 2 | 120
Results should be:
driver_id | total_mileage
1 | 80
2 | 70
My solution is:
SUM (mileage)
FROM (SELECT (odometer2-odometer) AS mileage
FROM (SELECT vehicle_id , odometer ,driver_id ,
NEXT.driver_id AS driver_id 2, NEXT.odometer AS odometer2 FROM Table
**JOIN NEXT ROW** AS NEXT
)
WHERE driver_id=driver_id2
)
GROUP BY driver_id
vehicle_id | driver_id | odometer | driver_id2 | odometer2 |mileage
1 | 1 | 10 | 1 | 20 | 10
1 | 1 | 20 | 2 | 20 | -
1 | 2 | 20 | 2 | 40 | 20
1 | 2 | 40 | 1 | 40 | -
1 | 1 | 40 | 1 | 50 | 10
1 | 1 | 50 | - | - | -
2 | 1 | 10 | 1 | 20 | 10
2 | 1 | 20 | 2 | 20 | -
2 | 2 | 20 | 2 | 30 | 10
2 | 2 | 30 | 1 | 30 | -
2 | 1 | 30 | 1 | 80 | 50
2 | 1 | 80 | 2 | 80 | -
2 | 2 | 80 | 2 | 120 | 40
2 | 2 | 120 | - | - | -
In Cloud Spanner function Row_number, OVER, LAG do not exist.
My question is how to join left next row in Cloud Spanner?
You're looking to PIVOT the data. This is not something that is doable directly in Cloud Spanner SQL - you'll need to post-process the data to affect the pivot.

Counting unique values in Google Spreadsheet based on multiple columns

Let's take this spreadsheet for example:
ID | StoreName | StoreID | CheckinTime | User
0 | w1 | 1 | 10:00 | user1
1 | w5 | 1 | 10:01 | user2
2 | w2 | 1 | 10:01 | user1
3 | w1 | 1 | 10:01 | user4
4 | w5 | 1 | 10:05 | user1
5 | w3 | 1 | 10:05 | user6
6 | w1 | 1 | 10:05 | user1
7 | w1 | 1 | 10:05 | user1
Is there a way to create a new column/tab/sheet to count all the unique checkins for a store. So let's say; StoreName "w1" is visited by "user1" 3 times and 1 time by "user4". The expected output will be 2 (2 unique visitors for "w1"). This is the output I would like to have:
ID | StoreName | uniqueCheckins
0 | w1 | 2
1 | w2 | 1
2 | w3 | 1
3 | w4 | 0
4 | w5 | 2
To produce the StoreName and uniqueCheckins output columns:
=QUERY(QUERY(B:E,"select B, E, count(C) group by B, E",1),"select Col1, count(Col2) group by Col1 label count(Col2) 'uniqueCheckins'",1)
However this will omit any StoreName that doesn't appear in the raw data (in your example, w4). Would this be OK?
Updated my answer
=({FILTER(SORT(UNIQUE(Index(UNIQUE({B2:B,E2:E}),,1))),SORT(UNIQUE(Index(UNIQUE({B2:B,E2:E}),,1)))<>""),ARRAYFORMULA(COUNTIF(INDEX(UNIQUE({B2:B,E2:E}),,1),"="&FILTER(SORT(UNIQUE(Index(UNIQUE({B2:B,E2:E}),,1))),SORT(UNIQUE(Index(UNIQUE({B2:B,E2:E}),,1)))<>"")))})

ios core data union query

Is it possible to do union of two queries (from the same entity) in core data? In SQL speak, if entity is called t, then consider that T has following data:
+------+------+------+
| x | y | z |
+------+------+------+
| 1 | 11 | 2 |
| 1 | 12 | 3 |
| 2 | 11 | 1 |
| 3 | 12 | 3 |
Then I am trying to run the following query (using core data - not SQLite)
select x, y, sum(z)
from t
group by 1, 2
union
select x, 1 as y, sum(z)
from t
group by 1, 2
order by x, y, 1
;
+------+------+--------+
| x | y | sum(z) |
+------+------+--------+
| 1 | 1 | 5 |
| 1 | 11 | 2 |
| 1 | 12 | 3 |
| 2 | 1 | 1 |
| 2 | 11 | 1 |
| 3 | 1 | 3 |
| 3 | 12 | 3 |
+------+------+--------+
7 rows in set (0.00 sec)
Is it possible?
Thanks!

Resources