Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8145473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:40:22+00:00 2026-06-06T13:40:22+00:00

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

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:40:24+00:00Added an answer on June 6, 2026 at 1:40 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

2 days I've been trying to get my head around this, but no joy.
Forgive the noob question, but I'm trying to get my head wrapped around this.
I can't wrap my head around this for some reason. I'm trying to get
I'm trying to get my head around a query and I just can't figure
Really trying to get my head around this but cannot see the light through
I'm trying to get my head around this behaviour: I have a ListView on
I have been trying to get my head around this but it hasn't been
Ok, so I've been trying to get my head around this, but I'm struggling.
Hey all. Ive got a headache trying to get my head around this. I
I am trying to get my head around NHibernate, I've run through this tutorial

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.