Google Sheets Query – How to Aggregate and Pivot Simultaneously

google sheetsgoogle-sheets-query

Hey I am trying to transform a table from this:

enter image description here

Into this:

enter image description here

Basically for each "Type" I want the totals for the different values for "Exists" which right now is just TRUE and FALSE.

But I can only get to this so far:

enter image description here

I did that using the following QUERY

SELECT B, C, COUNT(C) GROUP BY B,C

I know the next step requires a PIVOT but I'm having some fundamental misunderstanding about how PIVOT works to get anywhere. If you could help me out with some understanding and not just give me the answer it would be great.

Best Answer

The select argument of QUERY doesn't allow to use the same column to group, aggregate and to pivot data.

The workaround is to add an auxiliary column that repeats the data from C, let say it will be D, the select argument will be

=query(A1:D6,"SELECT B, COUNT(C) GROUP BY B PIVOT D")