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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:43:20+00:00 2026-06-11T12:43:20+00:00

I have three tables I need to join. The first two are easy and

  • 0

I have three tables I need to join. The first two are easy and based on the same key. A simple outer join will work. The third is tricky. It has a similar key but must be matched on the left 14 of one key. This produces duplicates which I do not want. Essentially, I want the TOP 1 FROM Table3 that matches the IDs correctly.

Using T-SQL.

The tables are large and complicated, so I’ve outlined below the a simple example of the tables with sample data and the query I’m running now.

Table1

ID1          ID2                 Field1    Field2    Field3    Field4
0000000000   00000000000000a     Info1     Info2     Info3     Info4
2222222222   11111111111111b     Info1     Info2     Info3     Info4
1111111111   22222222222222c     Info1     Info2     Info3     Info4

Table2

ID1          ID2                 Field1    Field2  
0000000000   00000000000000a     Info5     Info6  
2222222222   11111111111111b     Info5     Info6  
1111111111   22222222222222c     Info5     Info6 

Table

ID1          ID2                 Field1    
0000000000   00000000000000a     Info7 
0000000000   00000000000000b     Info7  
0000000000   00000000000000c     Info7  
2222222222   11111111111111b     Info7  
2222222222   11111111111111d     Info7  
1111111111   22222222222222c     Info7  

Query

SELECT
Table1.ID1,
Table1.ID2,
Table1.Field1,
Table1.Field2,
Table1.Field3,
Table1.Field4,
Table2.Field1,
Table2.Field2,
Table3.Field1
FROM Table1
LEFT JOIN Table2
ON Table1.ID1=Table2.ID2 AND Table1.ID2=Table2.ID2
LEFT JOIN Table3
ON Table1.ID1=Table3.ID2 AND LEFT(Table1.ID2,14)=LEFT(Table3.ID2,14)

The response I get is

0000000000 00000000000000a Info1 Info2 Info3 Info4 Info5 Info6 Info7
0000000000 00000000000000a Info1 Info2 Info3 Info4 Info5 Info6 Info7
0000000000 00000000000000a Info1 Info2 Info3 Info4 Info5 Info6 Info7
2222222222 11111111111111b Info1 Info2 Info3 Info4 Info5 Info6 Info7
2222222222 11111111111111b Info1 Info2 Info3 Info4 Info5 Info6 Info7
1111111111 22222222222222c Info1 Info2 Info3 Info4 Info5 Info6 Info7

What I want is

0000000000 00000000000000a Info1 Info2 Info3 Info4 Info5 Info6 Info7
2222222222 11111111111111b Info1 Info2 Info3 Info4 Info5 Info6 Info7
1111111111 22222222222222c Info1 Info2 Info3 Info4 Info5 Info6 Info7
  • 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-11T12:43:21+00:00Added an answer on June 11, 2026 at 12:43 pm

    I will take a guess and say you use mssql, (using the top and stuff) so this will work fine if you use 2005+

    SELECT
      Table1.ID1,
      Table1.ID2,
      Table1.Field1,
      Table1.Field2,
      Table1.Field3,
      Table1.Field4,
      Table2.Field1,
      Table2.Field2,
      t3.Field1
    FROM Table1
    LEFT JOIN Table2
      ON Table1.ID1=Table2.ID2 AND Table1.ID2=Table2.ID2
    outer apply (select top 1 * from table3 where LEFT(Table1.ID2,14)=LEFT(Table3.ID2,14)) t3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to join two tables based on columns with different types. The first
Need help with a query that I wrote: I have three tables Company id
Need some help with a query.. I have three tables. Source id name 1
I need help with a MySQL query. I have three tables: `product_category` ( `id`
I have three tables. First table is Activities and second is Members and last
I need to full join two tables with multiple keys As an example, I
I have two tables: t1 with the following columns: name | key | length
i have two tables: TblAchievements>> [ID, AchName] TblUsersAchievements>> [UserID, AchID] i need the results
I have three tables that I need get information from, 1 table has the
I have a database with three tables and I need to cross reference the

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.