4
votes

I am trying to get my head around this query - seems pretty straight forward

The table below captures which user watched which video

topic_user_id | topic_id | user_id
     1        |   10     |   3
     2        |   10     |   4
     3        |   11     |   5
     4        |   11     |   3
     5        |   12     |   6
     6        |   13     |   6
     7        |   14     |   7
     8        |   11     |   8
     9        |   11     |   9
     10       |   14     |   10
     11       |   15     |   11
     12       |   16     |   11
     13       |   17     |   11

Now to find out how many users watched a particular video - I have the following query.

HOW MANY USERS HAVE WATCHED A PARTICULAR VIDEO

select count(distinct(user_id)) as 'Number of Users',topic_id from topic_user
where user_id is not null
group by topic_id

Output

Number of Users | topic_id 
    2           |   10     
    4           |   11     
    1           |   12     
    1           |   13     
    2           |   14     
    1           |   15     
    1           |   16     
    1           |   17     

Read as: 2 users watched topic 10 , 4 watched topic 11 and so on

This works fine - But what I am looking for is to find :

How many users watched 1 video
How many users watched 2 videos
How many users watched 3 videos

The output should be something like

Number of Users  | Number of Videos watched
      6          |       1
      2          |       2
      1          |       3

Read as - 6 people watched only 1 video, 2 people watched 2 videos and so on.

Need some help with this.

Thanks in advance

5

5 Answers

2
votes

There may be an easier way but a subquery will work

select
    videos as 'Number of videos',
    count(user_id) as 'Num of Users'
from (
   select
        count(distinct(topic_id)) as videos,
        user_id from topic_user
   group by
        user_id
 ) sub
 group by
    videos
1
votes
select u.n as `Number of Video watched`, count(t.user_id)
from
    (select user_id, count(*) as `cnt`
    from topic_user 
    group by user_id) t INNER JOIN(
        SELECT 1 as `n` FROM DUAL 
        UNION 
        SELECT 2 as `n` FROM DUAL 
        UNION
        SELECT 3 as `n` FROM DUAL) u ON u.n = t.cnt
group by u.n

Fiddle: http://sqlfiddle.com/#!2/844ee/15

0
votes

Try this::

Select first_table.count(distinct(user_id))  as 'Number of Users', SUM(Count(topic_id))
from
(select count(distinct(user_id)) as 'Number of Users',topic_id from topic_user
where user_id is not null
group by topic_id) as first_table group by Count(topic_id)
0
votes

Try this

SELECT videos AS `Number of videos`,COUNT(user_id) AS `Num of Users`
FROM (SELECT COUNT(DISTINCT(topic_id)) AS videos,user_id FROM topic_user
GROUP BY user_id) sub_query
GROUP BY
videos
0
votes

"How many users watched n videos" could be interpreted two ways:

1) How many users watched n videos, regardless of title? The same video title may have been watched twice.

select videoCount, count(*) as userCount
  from (
    select count(*) as videoCount
      from topic_user
    group by user_id
  ) t
 group by videoCount

2) How many users watched n different video titles.

select videoCount, count(*) as userCount
  from (
    select count(distinct topic_id) as videoCount
      from topic_user
    group by user_id
  ) t
 group by videoCount