【问题标题】:Summing distinct number of elements attached to an index将附加到索引的不同数量的元素相加
【发布时间】:2021-02-13 00:45:32
【问题描述】:

给定一个关系:

-----
|x|y|
-----
|1|0|
|1|1|
|1|2|
|1|3|
|2|5|
|2|6|
-----

变成

-----
|x|z|
-----
|1|4|
|2|2|
-----

z 在哪里计算 sql 中索引 x 的不同条目?

【问题讨论】:

  • “不同的条目”是什么意思?

标签: sql count distinct aggregate-functions


【解决方案1】:

使用count(distinct):

select x, count(distinct y) z
from mytable
group by x

我根据您的问题描述添加了distinct,但您的示例数据并不能区分您是否真的需要它(y 中没有重复值)。 count(*) 在这里也可以正常工作。

【讨论】:

  • 谢谢,我是 SQL 新手,不知道 group by 运算符的存在!
猜你喜欢
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-12
  • 2013-02-10
  • 2017-08-06
  • 2013-11-07
  • 2015-08-31
相关资源
最近更新 更多