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

  • SEARCH
  • Home
  • 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 9101109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:02:10+00:00 2026-06-17T01:02:10+00:00

I have a few tables: DCDetails table which contains the Master data for a

  • 0

I have a few tables:

  1. “DCDetails” table which contains the Master data for a few diagnostics centers.
  2. “CompanyDetails” table which contains the Master data for Companies
  3. “Investigation” table which contains the Investigations(meaning set of medical tests to be conducted)

These are my master tables.

I also have a few mapping tables:
1. “CompanyDCMap” table which contains the MAPPING of Company to Diagnostic centers
2. “InvestigationDCMap” table which contains the MAPPING of Investigation to Diagnostic centers(Or DC for short)

I have to filter a set of DC based on two criteria which are:

  1. the DC belongs in the “CompanyDCMap” and
  2. Out of the DC filtered in (1), it also belongs in the “InvestigationDCMap” table.

How do I write the query for this so that I get the DC which are in both CompanyDCMap and InvestigationDCMap given I have the primary keys of “CompanyDetails” and “Investigation” tables.

I have almost given up, I am unable to think of a query which filters two sets at the same time.

Kindly help me.

UPDATE
Schema:
CompanyDetails table:

CompanyID(PRIMARY KEY), CompanyName(NVARCHAR(100))
   1                     Company1
   2                     Company2
   3                     Company3

Investigation Table:

InvestigationID(Primary key) , InvestigationName(NVARCHAR(100))
   1                               HIV+ Blood Test
   2                               TMT
   3                               Urine Test

DCDetails Table:

DCID(PRIMARY KEY), DCName(NVARCHAR(100))
   1                 DC1
   2                 DC2
   3                 DC3

CompanyDCMap table

   CompanyDCMapID(Primary key), CompanyID(Foreign key), DCId(Foreign Key)
          1                        1                       1
          2                        1                       2
          3                        2                       2
          4                        2                       3
          5                        3                       1
          6                        3                       3

InvestigationDCMap table

  InvestigationDCMapID(Primary Key), InvestigationID(Foreign Key), DCId(Foreign Key)
          1                                1                           1
          2                                1                           3
          3                                2                           2
          4                                2                           3

Expected Output of a query given CompanyID = 1 and InvestigationID = 2, SELECT DCId and DCName =

     DCId(Int)                   DCName(NVARCHAR(100))
      2                          DC2
  • 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-17T01:02:11+00:00Added an answer on June 17, 2026 at 1:02 am
    SELECT d.DCID, d.DCName
    FROM dbo.DCDetails AS d
    INNER JOIN dbo.CompanyDCMap AS c
    ON d.DCID = c.DCId
    INNER JOIN dbo.InvestigationDCMap AS i
    ON i.DCId = d.DCID;
    

    To get “distinct” values, you can use:

    SELECT DISTINCT d.DCID, d.DCName
    FROM dbo.DCDetails AS d
    INNER JOIN dbo.CompanyDCMap AS c
    ON d.DCID = c.DCId
    INNER JOIN dbo.InvestigationDCMap AS i
    ON i.DCId = d.DCID;
    

    Or…

    SELECT d.DCID, d.DCName
    FROM dbo.DCDetails AS d
    INNER JOIN dbo.CompanyDCMap AS c
    ON d.DCID = c.DCId
    INNER JOIN dbo.InvestigationDCMap AS i
    ON i.DCId = d.DCID
    GROUP BY d.DCID, d.DCName;
    

    Better yet, since no relationships with the other tables are needed:

    SELECT d.DCID, d.DCName
    FROM dbo.DCDetails AS d
    WHERE EXISTS (SELECT 1 FROM dbo.CompanyDCMap WHERE DCId = d.DCID)
    AND EXISTS (SELECT 1 FROM dbo.InvestigationDCMap WHERE DCId = d.DCID);
    

    This will be a much more efficient query, but if you need other columns from the other tables, you’ll need to revert to the join version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a database with a few tables. The first table contains the
Basically, I have a table which contains a few properties for a company. This
I have got a few tables which I am trying to join. I just
in my application I have a few CListCtrl tables. I fill/refresh them with data
I had a home page which have a few tables that refresh itself every
We have a few tables with persisted computed columns in SQL Server. Is there
I have a few tables with 70-80 columns in them. I would like to
I'm using Filemaker Pro 12 Advanced. I have a few tables but have really
I have a few thousand tables in my SQL Server instance, and I need
I have a database with few tables, ex.: Employee (Id, Name, state, country) Material

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.