mysql - SQL select query about grouping 2 columns -
i trying head around query - seems pretty straight forward
the table below captures user watched 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 find out how many users watched particular video - have following query.
how many users have watched particular video
select count(distinct(user_id)) 'number of users',topic_id topic_user user_id not null group 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 , on
this works fine - looking find :
how many users watched 1 video
how many users watched 2 videos
how many users watched 3 videos
the output should like
number of users | number of videos watched 6 | 1 2 | 2 1 | 3 read - 6 people watched 1 video, 2 people watched 2 videos , on.
need this.
thanks in advance
there may easier way subquery work
select videos 'number of videos', count(user_id) 'num of users' ( select count(distinct(topic_id)) videos, user_id topic_user group user_id ) sub group videos
Comments
Post a Comment