SQL - 选择不同值的计数

时间:2022-02-09 01:29:13

Assume I have table count and the column is (nilai, id_courses, id_lecturer)

假设我有表计数,列是(nilai,id_courses,id_lecturer)

nilai    id_courses    id_lecturer
-----    ----------    -----------
2        1             1
2        1             1
2        1             1
3        1             1
3        1             1
1        2             1
1        2             1
5        2             1
5        2             1

then I want to create view like this :

然后我想创建这样的视图:

nilai    id_courses    id_lecturer   count
-----    ----------    -----------   -----
2        1             1             3
3        1             1             2
1        2             1             2
5        2             1             2

how to do that in SQL syntax?

如何在SQL语法中做到这一点?

I just know how to count 1 value with this code

我只知道如何使用此代码计算1个值

SELECT COUNT( nilai ) , id_courses, id_lecturer FROM count where nilai=1

SELECT COUNT(nilai),id_courses,id_lecturer FROM count其中nilai = 1

I've read this post but its to complex, so I don't know how it's work

我已经读过这篇文章,但它很复杂,所以我不知道它是如何工作的

1 个解决方案

#1


2  

You need to count all distinct entries by grouping them. The query

您需要通过对所有不同的条目进行分组来计算它们。查询

SELECT nilai, id_courses, id_lecturer, COUNT(*) AS count
FROM count GROUP BY nilai, id_courses, id_lecturer

should exactly return the table you posted.

应该完全返回您发布的表格。

#1


2  

You need to count all distinct entries by grouping them. The query

您需要通过对所有不同的条目进行分组来计算它们。查询

SELECT nilai, id_courses, id_lecturer, COUNT(*) AS count
FROM count GROUP BY nilai, id_courses, id_lecturer

should exactly return the table you posted.

应该完全返回您发布的表格。