0

Assume I have table count and the column is (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?

I just know how to count 1 value with this code

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

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

3
  • Are you using MySQL or MS SQL Server? (Don't tag products not involved.) Commented Feb 19, 2017 at 10:40
  • 1
    Tip of today: check out GROUP BY, and the aggregate function COUNT(). Commented Feb 19, 2017 at 10:41
  • @jarlh Im using MySQL on hostinger, thanks for the tip, and maybe that answer below is work Commented Feb 19, 2017 at 10:45

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.