查询 table 中唯一属性的计数

Query a count of unique attributes in a table

我有一个 table 这样的:

+--------+-------+--------+-------+
| attr1  | attr2 | attr3  | attr4 |
+--------+-------+--------+-------+
| purple | wine  | clear  |  10.0 |
| red    | wine  | solid  |  20.0 |
| red    | beer  | cloudy |  10.0 |
| purple | ale   | clear  |  34.0 |
| blue   | ale   | solid  |  16.0 |
+--------+-------+--------+-------+

我想这样转换:

+--------+-------+-------+-------+-------+
|        | attr1 | attr2 | attr3 | attr4 |
+--------+-------+-------+-------+-------+
| purple |     2 |       |       |       |
| red    |     2 |       |       |       |
| blue   |     1 |       |       |       |
| wine   |       |     2 |       |       |
| beer   |       |     1 |       |       |
| ale    |       |     2 |       |       |
| clear  |       |       |     2 |       |
| solid  |       |       |     2 |       |
| cloudy |       |       |     1 |       |
| 10.0   |       |       |       |     2 |
| 20.0   |       |       |       |     1 |
| 34.0   |       |       |       |     1 |
| 16.0   |       |       |       |     1 |
+--------+-------+-------+-------+-------+

这个旋转或交叉table 将显示每个属性值在各自列中的计数。

如何使用Google查询语言显示这样的交叉table?

好吧,如果数据分为两列,那就很简单了,例如对于这样的事情

Attrib  Column
Red     1
Red     1
Green   1
Blue    1
Beer    2
Ale     2
Ale     2

你可以使用像

这样的查询
=query(A:B,"select A,count(A) where A<>'' group by A pivot  B")

所以问题是将 OP# 的数据组织成两列。

这可以通过目前相当标准的 split/join/transpose 技术来完成

=ArrayFormula(split(transpose(split(textjoin("|",true,if(A2:D="","",A2:D&" "&column(A2:D))),"|"))," "))

给予

您可以运行查询结果,也可以像这样组合两者

 =ArrayFormula(query({"Attrib","Number";split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" "&column(A2:D))),"|"))," ")},"Select Col1,count(Col1) group by Col1 pivot Col2"))

我已将列号加入到属性中,例如1-blue,以便它按正确的顺序排序。如果您不喜欢它,可以使用 regexreplace 删除它。

编辑

稍微短一点的公式 - 我不需要将 headers 单独放入:

=ArrayFormula(query(split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" Attr"&column(A2:D))),"|"))," "),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))

编辑 2

我在那里有点厚,应该使用 OP 数据的第一行作为属性标签而不是列号

=ArrayFormula(query(split(transpose(split(textjoin("|",true,if(A2:D="","",column(A2:D)&"-"&A2:D&" "&A1:D1)),"|"))," "),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))

编辑 3

应该选择一对更好的分隔符

    =ArrayFormula(query(split(transpose(split(textjoin("",true,if(A2:D="","",column(A2:D)&"-"&A2:D&""&A1:D1)),"")),""),
"Select Col1,count(Col1) group by Col1 pivot Col2",0))