Summing names with same values - google-sheets

I have an spreadsheet with one column with a bunch of names ( with duplicates) and a testing column which will be either 'ok', 'not - ok' or '' (if not started). I wanted to create a formula that would get all the unique names and then count how many 'not - ok' + '' that corresponds to that name so eg
Column A Column B
Bob ok
John not - ok
Rob
Bob not - ok
John ok
Joe ok
John
And the desired output would be
Column C Column D
Bob 1
John 2
Rob 1
Joe 0
I was able to get the unique name with
=UNIQUE(A2:A10) but not sure how to generate column D

this query gives everyone with 'ok' matched in column B:
=query(A2:B, "select A, count(B) where B matches '.*ok' group by A", 0)
if you like to make custom headers in this query, use this formula:
=query(A2:B,
"select A, count(B) where B matches '.*ok' group by A label A 'name1', count(B) 'name2'",
0)

Maybe try something like this:
=query(A2:B, "select A, count(B) where A <> '' and B <> 'ok' group by A", 0)
or
=query(A2:B, "select A, count(B) where A <> '' and (B = '' or B = 'not - ok') group by A", 0)

You can do it with pivot. "Names" goes in rows, as value you can use count of "column B" elements. Then add a global filter for the "ok" status

Related

Formula to return one of three values based on the contents of a range

I'm making a table to list the members of athletic teams, and I want to create a formula to determine if each team is male, female, or co-ed.
Team Name
Gender
A
Male
A
Male
A
Male
A
Male
B
Female
B
Female
B
Female
B
Female
C
Male
C
Male
C
Female
C
Female
I know I can use =countifs(A:A,"A",B:B,"Male") to count how many males are in team A, but I'm lost on how to output that Team A is male, Team B is female, and Team C is co-ed.
::Edit::
My desired result would be an output that states Team A is male, Team B is female, and Team C is co-ed.
Team
gender
A
Male
B
Female
C
Co-Ed
Use COUNTIFS to get the count of all males in each group. If it's
4, it's a all male group
0, it's all female group,
anything else is a co-ed group.
This creates redundant groups for each row. Use UNIQUE to remove all redundant groups.
=ARRAYFORMULA(UNIQUE({A2:A13,SWITCH(COUNTIFS(A2:A13,A2:A13,B2:B13,"Male"),4,"Male",0,"Female","Co-ed")}))
Group
Predominant type
A
Male
B
Female
C
Co-ed
the distribution can be achieved like this:
=QUERY(A:B, "select max(B) where B is not null group by B pivot A")
update
=QUERY(QUERY(
{QUERY(FILTER(FILTER(A:A, B:B="male", B:B<>"female"),
NOT(COUNTIF(FILTER(A:A, B:B<>"male", B:B="female"),
FILTER(A:A, B:B="male", B:B<>"female")))),
"select Col1,'Male',count(Col1) group by Col1");
QUERY(FILTER(FILTER(A:A, B:B<>"male", B:B="female"),
NOT(COUNTIF(FILTER(A:A, B:B="male", B:B<>"female"),
FILTER(A:A, B:B<>"male", B:B="female")))),
"select Col1,'Female',count(Col1) group by Col1");
QUERY({FILTER(FILTER(A:A, B:B<>"male", B:B="female"),
COUNTIF(FILTER(A:A, B:B="male", B:B<>"female"),
FILTER(A:A, B:B<>"male", B:B="female")));
FILTER(FILTER(A:A, B:B="male", B:B<>"female"),
COUNTIF(FILTER(A:A, B:B<>"male", B:B="female"),
FILTER(A:A, B:B="male", B:B<>"female")))},
"select Col1,'Co-Ed',count(Col1) group by Col1")}, "offset 1", ), "skipping 2", )
or:
=LAMBDA(x, y, QUERY(QUERY(
{QUERY(FILTER(FILTER(x, y="male", y<>"female"),
NOT(COUNTIF(FILTER(x, y<>"male", y="female"),
FILTER(x, y="male", y<>"female")))),
"select Col1,'Male',count(Col1) group by Col1");
QUERY(FILTER(FILTER(x, y<>"male", y="female"),
NOT(COUNTIF(FILTER(x, y="male", y<>"female"),
FILTER(x, y<>"male", y="female")))),
"select Col1,'Female',count(Col1) group by Col1");
QUERY({FILTER(FILTER(x, y<>"male", y="female"),
COUNTIF(FILTER(x, y="male", y<>"female"),
FILTER(x, y<>"male", y="female")));
FILTER(FILTER(x, y="male", y<>"female"),
COUNTIF(FILTER(x, y<>"male", y="female"),
FILTER(x, y="male", y<>"female")))},
"select Col1,'Co-Ed',count(Col1) group by Col1")},
"offset 1", ), "skipping 2", ))(A:A, B:B)
Supposing these 2 columns are A and B, you can add this to cell C1:
=UNIQUE(A:A)
This will populate the cells of column C with the unique values from column A.
For column D, there are 2 good options that work independent of how many team members are there.
The first is to consider as "Male" when there is no "Female" for the group and vice versa, and "Co-Ed" otherwise. Add this formula to D1:
=IF(C1="","",IF(COUNTIFS(A:A, C1, B:B, "Male")=0, "Female", IF(COUNTIFS(A:A, C1, B:B, "Female")=0, "Male", "Co-Ed")))
Copy cell D1 to the rest of column D. Blank groups will have blank results also. The result will be like this:
A
B
C
D
A
Male
A
Male
A
Male
B
Female
A
Male
C
Co-Ed
A
Male
B
Female
B
Female
B
Female
B
Female
C
Male
C
Male
C
Female
C
Female
Another option is to take directly the gender that corresponds to the group, and "Co-Ed" if there's more than one. Add this to D1 and copy to the rest of the column:
=IF(C1="";"";IF(COUNTA(UNIQUE(FILTER(B:B;A:A=C1)))=1;UNIQUE(FILTER(B:B;A:A=C1));"Co-Ed"))
The result is the same as above (but easier to extend to other similar cases).
The FILTER function brings the cells of the gender column based in the group column. UNIQUE reduces to different results. Hence, UNIQUE(FILTER(B:B;A:A=C1)) (varying C1 up to C3 or more automatically when copying) will result in Male for group A, Female for group B and 2 results (Male and Female) for group C. So we count the results with COUNTA: if only one, return it, if more, return "Co-Ed".

Query & importrange with a word that has symbol ' (example = James 'Lee)

I have issue to list the training program with the name that has symbol '. The participant name is James 'Lee
=QUERY(IMPORTRANGE("https://docs.google.com/spreadsheets...", "'record'!A:Z"), "Select Col7,Col18 where Col1 = '"&C5&"' and Col1 is not null", 0)
enter image description here
I received the error message:
Unable to parse query string for Function QUERY parameter 2: PARSE_ERROR: Encountered " "Lee "" at line 1, column 40. Was expecting one of: "group" ... "pivot" ... "order" ... "skipping" ... "limit" ... "offset" ... "label" ... "format" ... "options" ... "and" ... "or" ...
Try
=QUERY( IMPORTRANGE ("..., "...."), "Select Col7, Col18 where Col1 = """&C5&"""", 0)
and see if that works?
Select Col7,Col18 where Col1 like '"&regexreplace(C5,"\'","\%")&"' and Col1 is not null
will remove the hyphen and replace it with a wild card character %.
The side effect is that it will also bring back results like 'James OtherLee'

Using IF conditional in SELECT statement of Google Sheets Query function

In order to cleanse some outlying bad data I need to implement a conditional statement into the SELECT statement of a query in Google Sheets, but GS does not want to cooperate
My attempted statement:
=QUERY('JSON Data'!A1:AS,"Select A, IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) where B is not null GROUP BY A pivot B label IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) 'ratio' ",1)
Error thrown:
Unable to parse query string for Function QUERY parameter 2:
PARSE_ERROR: Encountered " "(" "( "" at line 1, column 13. Was
expecting one of: "where" ... "group" ... "pivot" ... "order"
... "skipping" ... "limit" ... "offset" ... "label" ... "format" ...
"options" ... "," ... "" ... "+" ... "-" ... "/" ... "%" ... "" ...
"/" ... "%" ... "+" ... "-" ...
Note that the following statement works:
Select A, SUM(AP)/SUM(AS) where B is not null GROUP BY A pivot B label SUM(AP)/SUM(AS) 'ratio'
The following statement does not work (testing to see if labelling the field has an effect):
Select A, IF(SUM(AP)/SUM(AS) > 1,1,SUM(AP)/SUM(AS)) where B is not null GROUP BY A pivot B
The sheet is here: https://docs.google.com/spreadsheets/d/1raQI22n3J08nKCHnz2LxRCXRP3dDnYJtGCEb8o4VUWY/edit#gid=1466522015
How to (Can I) execute a proper IF/THEN in the SELECT portion of a Google Sheets QUERY statement?
Try to process the if then before the query. See if this works
=ArrayFormula(QUERY({'JSON Data'!A1:B, IF('JSON Data'!AP1:AP/'JSON Data'!AS1:AS > 1, 1, 'JSON Data'!AP1:AP/'JSON Data'!AS1:AS) } ,"Select Col1, sum(Col3) where Col3 is not null GROUP BY Col1 pivot Col2",1))

Google Sheet query to select one column and do two count

I am trying to select column A, count column A and column B and group by A. But when I do that, count B return the same count as A.
Here is a sheet with an example:
https://docs.google.com/spreadsheets/d/1zpI3YjLYh9Mv2KCeuNk5TWtNKfyXpMb6OpGFlQtZ3Os/edit?usp=sharing
What I am aiming for here is for count(B) to be based on column B and not column A like it is right now.
You can use following formulas:
for City totals
=SORT(UNIQUE({A2:A;B2:B}))
for Pick up
=ArrayFormula(IF(E2:E<>"",COUNTIF(A:A,E2:E),""))
for Drop off
=ArrayFormula(IF(E2:E<>"",COUNTIF(B:B,E2:E),""))
I think you are looking for something like this:
=query({query(A2:B, "SELECT A, COUNT(A) WHERE A is not null GROUP BY A label A 'City Totals', COUNT(A) 'Pick Up' "),
query(A2:B, "SELECT B, COUNT(B) WHERE B is not null GROUP BY B label B 'City Totals', COUNT(B) 'Drop Off' ")},
"Select Col1,Col2,Col4")
Multiple formulas:
D2: =query(unique(flatten(A2:B)),"Select Col1 where Col1 is not NULL")
E2: =ARRAYFORMULA(IFERROR(VLOOKUP(D2:D15,query(A2:A, "SELECT A, COUNT(A) WHERE A is not null GROUP BY A label A 'City Totals', COUNT(A) 'Pick Up' "),{2},FALSE),0))
F2: =ARRAYFORMULA(IFERROR(VLOOKUP(D2:D15,query(B2:B, "SELECT B, COUNT(B) WHERE B is not null GROUP BY B label B 'City Totals', COUNT(B) 'Drop Off' "),{2},FALSE),0))

OFFSET an array

I'm using this formula to extract values from another sheet if certain criteria are met:
=QUERY(IMPORTRANGE("BLABLABLA", "Form Responses 1!A6:X"),"SELECT * WHERE Col4 <> '' AND Col2 = 'CONTENT' AND Col21 <> 'yes'")
What I'd like to do is display the content starting from column D of the input sheet. I can't "truncate" the IMPORTRANGE doing D6:X because I have a criteria for the QUERY referring to column B.
The dirty hack I'm using right now is just changing the offset number for each row where I want the output:
=INDEX(QUERY(IMPORTRANGE("BLABLABLA", "Form Responses 1!A6:X"),"SELECT * WHERE Col4 <> '' AND Col2 = 'CONTENT' AND Col21 <> 'yes'"),,4)
=INDEX(QUERY(IMPORTRANGE("BLABLABLA", "Form Responses 1!A6:X"),"SELECT * WHERE Col4 <> '' AND Col2 = 'CONTENT' AND Col21 <> 'yes'"),,5)
=INDEX(QUERY(IMPORTRANGE("BLABLABLA", "Form Responses 1!A6:X"),"SELECT * WHERE Col4 <> '' AND Col2 = 'CONTENT' AND Col21 <> 'yes'"),,6)
But it's a pain and I'm sure there's a way to output from the first formula directly the content only from column D to X (but still being able to use column B as a criterion for the QUERY function).
I had a look at the OFFSET function, but it seems it just outputs single cells, and doesn't expand an array.
What about using another query () ?
=QUERY(QUERY(IMPORTRANGE("BLABLABLA", "Form Responses 1!A6:X"),"SELECT * WHERE Col4 <> '' AND Col2 = 'CONTENT' AND Col21 <> 'yes'"),"SELECT Col4, Col5, Col6")

Resources