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 8103817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T23:39:33+00:00 2026-06-05T23:39:33+00:00

The title may not seem very clear – was not sure how to explain

  • 0

The title may not seem very clear – was not sure how to explain the problem in one line.

I have 3 tables

TOPIC
VIDEO 
TOPIC_VIDEO

A topic can have one or two videos. The videos are either sample videos or not.

Sample data in the tables with relevant column names

TOPIC

TOPIC_ID |         NAME          | COURSE_ID
    1    | Excel - Add/Subtract  |     1
    2    | Excel - sort          |     1
    3    | Excel - filter        |     1
    4    | Excel - formulas      |     1


VIDEO

VIDEO_ID | VIDEO_URL
    10   | www.youtube.com?v=123
    12   | www.youtube.com?v=345
    13   | www.youtube.com?v=567
    14   | www.youtube.com?v=879
    15   | www.youtube.com?v=443


TOPIC_VIDEO

TOPIC_VIDEO_ID | TOPIC_ID | VIDEO_ID | IS_SAMPLE    
      1        |    1    |    10    |    Y
      2        |    2    |    12    |    N
      3        |    3    |    13    |    N
      4        |    3    |    14    |    Y
      5        |    4    |    15    |    N

So what I am trying to do is for a given course select all topics and their corresponding videos. Now if a topic has more than one video – I want to select the video with is_sample as “N”.

Like in the above example – topic_id 3 has two videos with video_id 13 and 14 – so the video_id 13 should be selected.

This is the query I have so far

select topic.*,count(topic.topic_id),video.video_id,topic_video.is_sample
from topic
left join topic_video ON topic_video.topic_id = topic.topic_id
left join video ON video.video_id = topic_video.video_id
where course_id=1
group by topic.topic_id

So now if the count is 2 – I want to select the record with is_sample=’N’
Is this possible and how can I accomplish this.
Thanks a lot for your time

  • 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-05T23:39:34+00:00Added an answer on June 5, 2026 at 11:39 pm

    One way to solve this is to

    • Join to an inline view that calculates the count by topic.
    • Use the count to create a case statement that only joins to IS_SAMPLE = N

    SELECT * 
    FROM   topic 
           LEFT JOIN (SELECT topic_id, 
                             Count(topic_id) t_count 
                      FROM   TOPIC_VIDEO 
                      GROUP  BY topic_id) t 
                  ON topic.topic_id = t.topic_id 
           LEFT JOIN topic_video 
                  ON topic_video.topic_id = topic.topic_id 
                     AND TOPIC_VIDEO.IS_SAMPLE = CASE 
                                                   WHEN t.t_count = 2 THEN 'N' 
                                                   ELSE TOPIC_VIDEO.IS_SAMPLE 
                                                 END 
           LEFT JOIN video 
                  ON video.video_id = topic_video.video_id 
    

    DEMO

    Output

    | TOPIC_ID |                 NAME | COURSE_ID | T_COUNT | TOPIC_VIDEO_ID | VIDEO_ID | IS_SAMPLE |             VIDEO_URL |
    -------------------------------------------------------------------------------------------------------------------------
    |        1 | Excel - Add/Subtract |         1 |       1 |              1 |       10 |         Y | www.youtube.com?v=123 |
    |        2 |         Excel - sort |         1 |       1 |              2 |       12 |         N | www.youtube.com?v=345 |
    |        3 |       Excel - filter |         1 |       2 |              3 |       13 |         N | www.youtube.com?v=567 |
    |        4 |     Excel - formulas |         1 |       1 |              5 |       15 |         N | www.youtube.com?v=443 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry the title may not be very clear. Basically I have a wrapper for
The title may have been confusing, but please let me explain: Currently when I
This title may seem strange, so let me try to explain what I'm trying
Ok, the title may not be the most descriptive. It's easier to explain with
my title may not explain what I am looking for, this is my current
This may seem like a silly title but I'm running into a small problem
Okay the question title may not have made sense... mostly because I don't know
In advance, I apologize if the question title may not be be convenient. I'm
The title may seem a little weird but what I find around the internet
I know, I know, the title of my message may seem provocative, since boost::mutex

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.