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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:17:18+00:00 2026-05-11T14:17:18+00:00

My goal is to get a query written. I have three tables, A, B

  • 0

My goal is to get a query written. I have three tables, A, B and C. The tables are written such that A.bID = B.bID, and B.cID = C.cID. This basically allows me to write a query where I link a record from a to b, and link the b record to a record from c. So far so good, simple query.

What my problem is… one of the columns included in the query (let’s call it C.col3) has to have unique values; the values in this column can only show up once in the query result, but other columns from the other tables do not have this requirement.

Can anybody help me write this query?

Thanks…

Update 1:

Here is the table layout (sorry, I have to use generic names)

Table A
aID, bID, aCol1, aCol2, aCol3 … aCol10

Table B
bID, cID, bCol1, bCol2, bCol3 … bCol10

Table C
cID, cCol1, cCol2, col3, cCol4 … cCol10

Without the unique value constraint in col3, I would write the query like this:

SELECT     A.aID, A.bID, A.aCol1 ... A.aCol10,     B.bID, B.cID, B.bCol1 ... B.bCol10,     C.cID, C.cCol1, C.cCol2, C.col3 ... C.cCol10 FROM     A, B, C WHERE      A.bID = B.bID AND B.cID = C.cID 

… but of course that doesn’t make sure the that values in C.col3 are unique.

Update 2: More info…
Table A and Table B have a one to many relationship; A is the ‘header’, B is the ‘item’.
Table B and Table C have a one to one relationship.

These tables are part of a caching mechanism, so lots of data that looks similar, but is still different in some cols.

Since A is the header, most of the duplicate values will be found in A.

I first need to order the rows by A.aID, but then after that I only need the first rows returned, where the value for C.col3 does not appear in a previous row for that col.

Does that make things a little clearer, or am I still not making any sense? 🙂

Final Update:

I chose Bartosz Klimek’s answer as it was the closest to what I needed; I just had to modify the nested join clause in the middle.

Thank you all for your help!

  • 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. 2026-05-11T14:17:18+00:00Added an answer on May 11, 2026 at 2:17 pm
    SELECT A.*, B.*, C.*   FROM C     JOIN B ON B.cID = C.cID     JOIN A ON A.bID = B.bID     JOIN     (       SELECT id = min(aID)         FROM C           JOIN B ON B.cID = C.cID           JOIN A ON A.bID = B.bID         GROUP BY col3     ) D ON D.id = A.aID 

    Note that the subquery at the end assures that for each col3 value you will have at most one record in the final resultset. The record selected is the one with the minimal aID. Obviously I assume that aID, bID and cID are primary keys of A, B and C, respectively.

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

Sidebar

Related Questions

The goal of this query is to get a total of unique records (most
I have this scenario wherein I get a linear equation in the Prolog query
I have a SQL query that has me stumped. Basically, I have a Recipes
My goal is to get a Time instance from a DateTime instance This has
My goal is to get various SEO-relevant information for various sites. Basically all the
How do i do that? Actually my main goal is to get which checkbox
I am using the following query to get the count from multiple tables:- SELECT
I have a view defined in SQL server 2008 that joins 4 tables together.
So I have written this code here: highlighter: function (item) { var parts =
Goal: Get values which are unique in $resultSecond So I have two objects. $result

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.