计算 SQL 中列值的唯一组合之后的行数

Counting number of rows that follow unique combinations of columns values in SQL

我正在研究 Apache Zeppelin。 我有以下 table:

|address | metric1 | metric2 | metric3|
|--------|---------|---------|--------|
|v       |        1|       1 |     200|
|w       |        1|       0 |     200|
|x       |        0|       1 |     200|
|y       |        0|       1 |     200|
|z       |        1|       0 |       1|

我想创建一个查询,显示指标值的所有可能组合以及每个组合结束的地址数。

像这样:

| metric1 | metric2 | metric3| count|
|---------|---------|--------|------|
|        1|       1 |     200|     1|
|        1|       0 |     200|     1|
|        0|       1 |     200|     2|
|        1|       0 |       1|     1|

我尝试了以下查询:

select metric1, metric2, metric3, count(*) as cnt 
from
(select distinct metric1, metric2, metric3 from table) as t;

但它会导致此错误: org.apache.hive.service.cli.HiveSQLException:编译语句时出错:SemanticException [错误 10025]:表达式不在 GROUP BY 键中 'metric1'

怎么了?

你只需要 GROUP BY:

select metric1, metric2, metric3, count(*) as cnt 
from t
group by metric1, metric2, metric3;

不需要子查询。