繁体   English   中英

如何编写COUNT(DISTINCT())查询?

[英]how to write COUNT(DISTINCT()) query?

请帮我这个查询。

我有一个餐桌学生,看起来像:

*id, name, surname, year, course*
1, John, Johnson, 3, Economy
2, Lara, Croft, 2, Biology
3, Mark, Jones, 3, Economy
4, Jim, Smith, 1, IT
5, Sarah, Kennedy, 1, IT
6, Matt, Damon, 3, Economy

并希望获得所有学生参加课程的结果集,例如:

*course, count*
Economy, 3
IT, 2
Biology, 1

您不会为此使用count(distinct ...) ,只需对课程进行分组并使用count()

select
  cource,
  count(*)
from
  Students
group by
  cource
order by
  cource

如果要在组中进行不同的计数,例如,计算year的不同值(在所有记录的隐式组中count(distinct ...)则使用count(distinct ...)很有用:

select
  count(distinct year)
from
  Students

这将使你的结果3 ,因为有三个不同的值123

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM