L
L
Lavrov952018-03-29 19:35:36
SQL
Lavrov95, 2018-03-29 19:35:36

How can I make sure that if the user_id is repeated, then not add it to the count?

such a database

id      |    user_id     |       level_id
1       |        5       |           7
2       |        5       |           6
3       |        4       |           9

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Ruslan., 2018-03-29
@Lavrov95

You can also do this:
select count(distinct user_id) as cnt
from table

A
alexalexes, 2018-03-29
@alexalexes

Until you say what you want to count, and on which DBMS, the number of records by unique user_id will look something like this:

select count(*) as cnt
from (select user_id
            from table
     group by user_id)

If you need to display some more nuances in the selection, then the query will change dramatically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question